Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 2 additions & 10 deletions sdk/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package sdk

import (
"log/slog"

"github.com/opentdf/platform/sdk/internal/oauth"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
Expand All @@ -16,7 +14,7 @@ type config struct {
clientCredentials oauth.ClientCredentials
tokenEndpoint string
scopes []string
unwrapper Unwrapper
authConfig *AuthConfig
}

func (c *config) build() []grpc.DialOption {
Expand All @@ -35,12 +33,6 @@ func WithClientCredentials(clientID, clientSecret string, scopes []string) Optio
return func(c *config) {
c.clientCredentials = oauth.ClientCredentials{ClientId: clientID, ClientAuth: clientSecret}
c.scopes = scopes
// Build kas client here to unblock sdk initialization. This will be refactored in the future.
uw, err := buildKASClient(c)
if err != nil {
slog.Error("failed to build KAS client", slog.String("error", err.Error()))
}
c.unwrapper = &uw
}
}

Expand All @@ -56,6 +48,6 @@ func WithTokenEndpoint(tokenEndpoint string) Option {
// make these options more robust
func WithAuthConfig(authConfig AuthConfig) Option {
return func(c *config) {
c.unwrapper = &authConfig
c.authConfig = &authConfig
}
}
13 changes: 11 additions & 2 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ func New(platformEndpoint string, opts ...Option) (*SDK, error) {
opt(cfg)
}

unwrapper := cfg.unwrapper
var unwrapper Unwrapper
if cfg.authConfig == nil {
uw, err := buildKASClient(cfg)
if err != nil {
return nil, err
}
unwrapper = &uw
} else {
unwrapper = cfg.authConfig
}

conn, err := grpc.Dial(platformEndpoint, cfg.build()...)
if err != nil {
Expand Down Expand Up @@ -82,7 +91,7 @@ func buildKASClient(c *config) (KASClient, error) {

// at this point we have either both client credentials and a token endpoint or none of the above
if c.clientCredentials.ClientId == "" {
return KASClient{}, errors.New("cannot create an SDK with no client credentials")
return KASClient{}, nil
}

ts, err := NewIDPAccessTokenSource(
Expand Down
12 changes: 12 additions & 0 deletions sdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func Test_ShouldCreateNewSDK(t *testing.T) {
}
}

func Test_ShouldCreateNewSDK_NoCredentials(t *testing.T) {
// When
sdk, err := sdk.New(goodPlatformEndpoint)
// Then
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if sdk == nil {
t.Errorf("Expected sdk, got nil")
}
Comment thread
mkleene marked this conversation as resolved.
Outdated
}

func Test_ShouldCloseSDKConnection(t *testing.T) {
t.Skip("Skipping test since close is broken")
// Given
Expand Down