Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gRPC client auth added #1084

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion pkg/bot/cloudslack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/slack-go/slack/slackevents"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"

"github.com/kubeshop/botkube/internal/config/remote"
"github.com/kubeshop/botkube/pkg/api"
Expand All @@ -28,6 +29,11 @@ import (
"github.com/kubeshop/botkube/pkg/sliceutil"
)

const (
APIKeyContextKey = "X-Api-Key" // #nosec
DeploymentIDContextKey = "X-Deployment-Id" // #nosec
)

var _ Bot = &CloudSlack{}

// CloudSlack listens for user's message, execute commands and sends back the response.
Expand Down Expand Up @@ -92,7 +98,10 @@ func NewCloudSlack(log logrus.FieldLogger,

func (b *CloudSlack) Start(ctx context.Context) error {
creds := grpc.WithTransportCredentials(insecure.NewCredentials())
opts := []grpc.DialOption{creds}
opts := []grpc.DialOption{creds,
grpc.WithStreamInterceptor(b.addStreamingClientCredentials()),
grpc.WithUnaryInterceptor(b.addUnaryClientCredentials()),
}

conn, err := grpc.Dial(b.cfg.Server.URL, opts...)
if err != nil {
Expand Down Expand Up @@ -515,3 +524,41 @@ func (b *CloudSlack) getChannelsToNotify(sourceBindings []string) []string {
}
return out
}

func (b *CloudSlack) addStreamingClientCredentials() grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
remoteCfg, ok := remote.GetConfig()
if !ok {
return nil, errors.New("empty remote configuration")
}
md := metadata.New(map[string]string{
APIKeyContextKey: remoteCfg.APIKey,
DeploymentIDContextKey: remoteCfg.Identifier,
})

ctx = metadata.NewOutgoingContext(ctx, md)

clientStream, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
return nil, err
}

return clientStream, nil
}
}

func (b *CloudSlack) addUnaryClientCredentials() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
remoteCfg, ok := remote.GetConfig()
if !ok {
return errors.New("empty remote configuration")
}
md := metadata.New(map[string]string{
APIKeyContextKey: remoteCfg.APIKey,
DeploymentIDContextKey: remoteCfg.Identifier,
})

ctx = metadata.NewOutgoingContext(ctx, md)
return invoker(ctx, method, req, reply, cc, opts...)
}
}