Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
Fix all system auths are assumed to be OneTimeTokens (#2046)
Browse files Browse the repository at this point in the history
* Fix all system auths are assumed to be OneTimeTokens

* Update values.yaml

* Check imports
  • Loading branch information
dzahariev authored Sep 30, 2021
1 parent 4d6ffd6 commit 666ba61
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
2 changes: 1 addition & 1 deletion chart/compass/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ global:
version: "PR-2027"
director:
dir:
version: "PR-2040"
version: "PR-2046"
gateway:
dir:
version: "PR-2027"
Expand Down
24 changes: 13 additions & 11 deletions components/director/internal/domain/application/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,25 +553,27 @@ func (r *Resolver) Auths(ctx context.Context, obj *graphql.Application) ([]*grap

out := make([]*graphql.AppSystemAuth, 0, len(sysAuths))
for _, sa := range sysAuths {
isTokenType := r.sysAuthSvc.IsSystemAuthOneTimeTokenType(&sa)
if _, err := r.oneTimeTokenSvc.IsTokenValid(&sa); isTokenType && err != nil {
log.C(ctx).WithError(err).Errorf("skipping one-time token due to its expiration or usage")
continue
}

c, err := r.sysAuthConv.ToGraphQL(&sa)
if err != nil {
return nil, err
}

if sa.Value.OneTimeToken != nil && sa.Value.OneTimeToken.Type == tokens.ApplicationToken {
oneTimeTokenForApplication, err := r.oneTimeTokenConv.ToGraphQLForApplication(*sa.Value.OneTimeToken)
if err != nil {
return nil, errors.Wrap(err, "while converting one-time token to graphql")
if r.sysAuthSvc.IsSystemAuthOneTimeTokenType(&sa) {
if valid, err := r.oneTimeTokenSvc.IsTokenValid(&sa); !valid {
log.C(ctx).WithError(err).Errorf("skipping one-time token due to its expiration or usage")
continue
}

c.(*graphql.AppSystemAuth).Auth.OneTimeToken = &oneTimeTokenForApplication
if sa.Value.OneTimeToken.Type == tokens.ApplicationToken {
oneTimeTokenForApplication, err := r.oneTimeTokenConv.ToGraphQLForApplication(*sa.Value.OneTimeToken)
if err != nil {
return nil, errors.Wrap(err, "while converting one-time token to graphql")
}

c.(*graphql.AppSystemAuth).Auth.OneTimeToken = &oneTimeTokenForApplication
}
}

out = append(out, c.(*graphql.AppSystemAuth))
}

Expand Down
37 changes: 34 additions & 3 deletions components/director/internal/domain/application/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,9 @@ func TestResolver_Auths(t *testing.T) {
txGen := txtest.NewTransactionContextGenerator(testError)

sysAuthModels := []model.SystemAuth{{ID: "id1", AppID: &id, Value: &auth}, {ID: "id2", AppID: &id, Value: &auth}}
sysAuthModelCert := []model.SystemAuth{{ID: "id1", AppID: &id, Value: nil}}
sysAuthGQL := []*graphql.AppSystemAuth{{ID: "id1", Auth: &graphql.Auth{}}, {ID: "id2", Auth: &graphql.Auth{}}}
sysAuthGQLCert := []*graphql.AppSystemAuth{{ID: "id1", Auth: nil}}
sysAuthExpected := []*graphql.AppSystemAuth{{ID: "id1", Auth: &graphql.Auth{OneTimeToken: &gqlAuth}}, {ID: "id2", Auth: &graphql.Auth{OneTimeToken: &gqlAuth}}}
emptySysAuth := make([]*graphql.AppSystemAuth, 0)
testCases := []struct {
Expand Down Expand Up @@ -1375,6 +1377,34 @@ func TestResolver_Auths(t *testing.T) {
ExpectedResult: sysAuthExpected,
ExpectedErr: nil,
},
{
Name: "Success when System Auth is certificate",
TransactionerFn: txGen.ThatSucceeds,
ServiceFn: func() *automock.SystemAuthService {
svc := &automock.SystemAuthService{}
svc.On("ListForObject", txtest.CtxWithDBMatcher(), model.ApplicationReference, id).Return(sysAuthModelCert, nil).Once()
svc.On("IsSystemAuthOneTimeTokenType", &sysAuthModelCert[0]).Return(false).Once()
return svc
},
SysAuthConvFn: func() *automock.SystemAuthConverter {
sysAuthConv := &automock.SystemAuthConverter{}
sysAuthConv.On("ToGraphQL", &sysAuthModelCert[0]).Return(sysAuthGQLCert[0], nil).Once()
return sysAuthConv
},
TokenSvcFn: func() *automock.OneTimeTokenService {
svc := &automock.OneTimeTokenService{}
svc.AssertNotCalled(t, "IsTokenValid")
return svc
},
TokenConvFn: func() *automock.TokenConverter {
conv := &automock.TokenConverter{}
conv.AssertNotCalled(t, "ToGraphQLForApplication")
return conv
},
InputApp: gqlApp,
ExpectedResult: sysAuthGQLCert,
ExpectedErr: nil,
},
{
Name: "Returns nothing when tokens are invalid",
TransactionerFn: txGen.ThatSucceeds,
Expand All @@ -1387,7 +1417,8 @@ func TestResolver_Auths(t *testing.T) {
},
SysAuthConvFn: func() *automock.SystemAuthConverter {
sysAuthConv := &automock.SystemAuthConverter{}
sysAuthConv.AssertNotCalled(t, "ToGraphQL")
sysAuthConv.On("ToGraphQL", &sysAuthModels[0]).Return(sysAuthGQL[0], nil).Once()
sysAuthConv.On("ToGraphQL", &sysAuthModels[1]).Return(sysAuthGQL[1], nil).Once()
return sysAuthConv
},
TokenSvcFn: func() *automock.OneTimeTokenService {
Expand Down Expand Up @@ -1466,7 +1497,7 @@ func TestResolver_Auths(t *testing.T) {
ServiceFn: func() *automock.SystemAuthService {
svc := &automock.SystemAuthService{}
svc.On("ListForObject", txtest.CtxWithDBMatcher(), model.ApplicationReference, id).Return(sysAuthModels, nil).Once()
svc.On("IsSystemAuthOneTimeTokenType", &sysAuthModels[0]).Return(true).Once()
svc.AssertNotCalled(t, "IsSystemAuthOneTimeTokenType")
return svc
},
SysAuthConvFn: func() *automock.SystemAuthConverter {
Expand All @@ -1476,7 +1507,7 @@ func TestResolver_Auths(t *testing.T) {
},
TokenSvcFn: func() *automock.OneTimeTokenService {
svc := &automock.OneTimeTokenService{}
svc.On("IsTokenValid", &sysAuthModels[0]).Return(true, nil).Once()
svc.AssertNotCalled(t, "IsTokenValid")
return svc
},
TokenConvFn: func() *automock.TokenConverter {
Expand Down
7 changes: 5 additions & 2 deletions components/director/internal/domain/systemauth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,11 @@ func (s *service) DeleteByIDForObject(ctx context.Context, objectType model.Syst

// IsSystemAuthOneTimeTokenType missing godoc
func (s *service) IsSystemAuthOneTimeTokenType(systemAuth *model.SystemAuth) bool {
if (systemAuth != nil && systemAuth.Value != nil) &&
(systemAuth.Value.Credential.Basic != nil || systemAuth.Value.Credential.Oauth != nil || systemAuth.Value.RequestAuth != nil) {
if systemAuth == nil || systemAuth.Value == nil {
return false
}

if systemAuth.Value.Credential.Basic != nil || systemAuth.Value.Credential.Oauth != nil || systemAuth.Value.RequestAuth != nil {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion components/director/pkg/graphql/one_time_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type OneTimeTokenForApplication struct {
}

// IsOneTimeToken missing godoc
func (OneTimeTokenForApplication) IsOneTimeToken() {}
func (t *OneTimeTokenForApplication) IsOneTimeToken() {}

// OneTimeTokenForRuntime missing godoc
type OneTimeTokenForRuntime struct {
Expand Down
2 changes: 0 additions & 2 deletions components/director/pkg/graphql/schema_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 666ba61

Please sign in to comment.