Skip to content

Commit

Permalink
feat: convert library options to internal options
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Dec 20, 2023
1 parent 6cc53b8 commit ec62371
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/token/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const (
)

// Options defines the options for getting token.
// This struct is a subset of internal/token.Options where its values are copied
// to internal type. See internal/token/options.go for details
type Options struct {
LoginMethod string

Expand All @@ -27,7 +29,7 @@ type Options struct {
ClientSecret string
ClientCert string
ClientCertPassword string
IsPopTokenEnabled bool
IsPoPTokenEnabled bool
PoPTokenClaims string

// for ROPCLogin
Expand Down
21 changes: 21 additions & 0 deletions pkg/token/options_ctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"github.com/Azure/kubelogin/pkg/internal/env"
"github.com/Azure/kubelogin/pkg/internal/token"
)

// OptionsWithEnv loads options from environment variables.
Expand Down Expand Up @@ -44,3 +45,23 @@ func OptionsWithEnv() *Options {

return rv
}

func (opts *Options) toInternalOptions() *token.Options {
return &token.Options{
LoginMethod: opts.LoginMethod,
Environment: opts.Environment,
TenantID: opts.TenantID,
ServerID: opts.ServerID,
ClientID: opts.ClientID,
ClientSecret: opts.ClientSecret,
ClientCert: opts.ClientCert,
ClientCertPassword: opts.ClientCertPassword,
IsPoPTokenEnabled: opts.IsPoPTokenEnabled,
PoPTokenClaims: opts.PoPTokenClaims,
Username: opts.Username,
Password: opts.Password,
IdentityResourceID: opts.IdentityResourceID,
AuthorityHost: opts.AuthorityHost,
FederatedTokenFile: opts.FederatedTokenFile,
}
}
86 changes: 86 additions & 0 deletions pkg/token/options_ctor_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package token

import (
"reflect"
"testing"

"github.com/Azure/kubelogin/pkg/internal/env"
"github.com/Azure/kubelogin/pkg/internal/token"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -81,3 +83,87 @@ func TestOptionsWithEnv(t *testing.T) {
}, o)
})
}

func TestOptions_toInternalOptions(t *testing.T) {
t.Run("basic", func(t *testing.T) {
o := &Options{
LoginMethod: "login-method",
Environment: "environment",
TenantID: "tenant-id",
ServerID: "server-id",
ClientID: "client-id",
ClientSecret: "client-secret",
ClientCert: "client-cert",
ClientCertPassword: "client-cert-password",
IsPoPTokenEnabled: true,
PoPTokenClaims: "pop-token-claims",
Username: "username",
Password: "password",
IdentityResourceID: "identity-resource-id",
AuthorityHost: "authority-host",
FederatedTokenFile: "federated-token-file",
}
assert.Equal(t, &token.Options{
LoginMethod: "login-method",
Environment: "environment",
TenantID: "tenant-id",
ServerID: "server-id",
ClientID: "client-id",
ClientSecret: "client-secret",
ClientCert: "client-cert",
ClientCertPassword: "client-cert-password",
IsPoPTokenEnabled: true,
PoPTokenClaims: "pop-token-claims",
Username: "username",
Password: "password",
IdentityResourceID: "identity-resource-id",
AuthorityHost: "authority-host",
FederatedTokenFile: "federated-token-file",
}, o.toInternalOptions())
})


// this test uses reflection to ensure all fields in *Options
// are copied to *token.Options without modification.
t.Run("fields assignment", func(t *testing.T) {
boolValue := true
stringValue := "string-value"

o := &Options{}

// fill up all fields in *Options
oType := reflect.TypeOf(o).Elem()
oValue := reflect.ValueOf(o).Elem()
for i := 0; i < oValue.NumField(); i++ {
fieldValue := oValue.Field(i)
fieldType := oType.Field(i)
switch k := fieldType.Type.Kind(); k {
case reflect.Bool:
// set bool value
fieldValue.SetBool(boolValue)
case reflect.String:
fieldValue.SetString(stringValue)
default:
t.Errorf("unexpected type: %s", k)
}
}

internalOpts := o.toInternalOptions()
assert.NotNil(t, internalOpts)

internalOptsValue := reflect.ValueOf(internalOpts).Elem()
for i := 0; i < oValue.NumField(); i++ {
fieldType := oType.Field(i)
t.Log(fieldType.Name)
internalOptsFieldValue := internalOptsValue.FieldByName(fieldType.Name)
switch k := fieldType.Type.Kind(); k {
case reflect.Bool:
assert.Equal(t, boolValue, internalOptsFieldValue.Bool(), "field: %s", fieldType.Name)
case reflect.String:
assert.Equal(t, stringValue, internalOptsFieldValue.String(), "field: %s", fieldType.Name)
default:
t.Errorf("unexpected type: %s", k)
}
}
})
}

0 comments on commit ec62371

Please sign in to comment.