-
Notifications
You must be signed in to change notification settings - Fork 174
ROX-20100: Add machine to machine auth api #8145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
618ed24
rox-20100-machine-to-machine-api
dhaus67 594fbec
Fix API naming.
dhaus67 c8e9809
Address review comments.
dhaus67 43d4328
Re-generate code.
dhaus67 098437f
Add ReferencedByAnotherObject errox in postgres.
dhaus67 9502348
Ignore auth store tests due to FK constraint.
dhaus67 e2e5c81
Address review comments.
dhaus67 2f65d39
Address review comments.
dhaus67 e77a1f6
Fix tests.
dhaus67 ab07e16
Move issuer to separate field and make it unique.
dhaus67 f6cfb9f
Address review comments.
dhaus67 fc7f48d
Address review comments.
dhaus67 7297085
Re-generate code.
dhaus67 b281c32
Fix convert tests.
dhaus67 f4b7f60
Address review comments.
dhaus67 cacd1bd
Address review comments.
dhaus67 01cb0e3
Address review comments.
dhaus67 b1b0bf2
Address review comments.
dhaus67 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package datastore | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/stackrox/rox/central/auth/store" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| ) | ||
|
|
||
| // DataStore for auth machine to machine configs. | ||
| type DataStore interface { | ||
| GetAuthM2MConfig(ctx context.Context, id string) (*storage.AuthMachineToMachineConfig, bool, error) | ||
| ListAuthM2MConfigs(ctx context.Context) ([]*storage.AuthMachineToMachineConfig, error) | ||
| AddAuthM2MConfig(ctx context.Context, config *storage.AuthMachineToMachineConfig) (*storage.AuthMachineToMachineConfig, error) | ||
| UpdateAuthM2MConfig(ctx context.Context, config *storage.AuthMachineToMachineConfig) error | ||
| RemoveAuthM2MConfig(ctx context.Context, id string) error | ||
| } | ||
|
|
||
| // New returns an instance of an auth machine to machine Datastore. | ||
| func New(store store.Store) DataStore { | ||
| return &datastoreImpl{store: store} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package datastore | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/stackrox/rox/central/auth/store" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| ) | ||
|
|
||
| var ( | ||
| _ DataStore = (*datastoreImpl)(nil) | ||
| ) | ||
|
|
||
| type datastoreImpl struct { | ||
| store store.Store | ||
| } | ||
|
|
||
| func (d *datastoreImpl) GetAuthM2MConfig(ctx context.Context, id string) (*storage.AuthMachineToMachineConfig, bool, error) { | ||
| return d.store.Get(ctx, id) | ||
| } | ||
|
|
||
| func (d *datastoreImpl) ListAuthM2MConfigs(ctx context.Context) ([]*storage.AuthMachineToMachineConfig, error) { | ||
| return d.store.GetAll(ctx) | ||
| } | ||
|
|
||
| func (d *datastoreImpl) AddAuthM2MConfig(ctx context.Context, config *storage.AuthMachineToMachineConfig) (*storage.AuthMachineToMachineConfig, error) { | ||
| if err := d.store.Upsert(ctx, config); err != nil { | ||
| return nil, err | ||
| } | ||
| return config, nil | ||
| } | ||
|
|
||
| func (d *datastoreImpl) UpdateAuthM2MConfig(ctx context.Context, config *storage.AuthMachineToMachineConfig) error { | ||
| if err := d.store.Upsert(ctx, config); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
dhaus67 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (d *datastoreImpl) RemoveAuthM2MConfig(ctx context.Context, id string) error { | ||
| return d.store.Delete(ctx, id) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| //go:build sql_integration | ||
|
|
||
| package datastore | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| pgStore "github.com/stackrox/rox/central/auth/store/postgres" | ||
| roleDataStore "github.com/stackrox/rox/central/role/datastore" | ||
| permissionSetPostgresStore "github.com/stackrox/rox/central/role/store/permissionset/postgres" | ||
| rolePostgresStore "github.com/stackrox/rox/central/role/store/role/postgres" | ||
| accessScopePostgresStore "github.com/stackrox/rox/central/role/store/simpleaccessscope/postgres" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/pkg/errox" | ||
| "github.com/stackrox/rox/pkg/postgres/pgtest" | ||
| "github.com/stackrox/rox/pkg/sac" | ||
| "github.com/stackrox/rox/pkg/sac/resources" | ||
| "github.com/stackrox/rox/pkg/set" | ||
| "github.com/stackrox/rox/pkg/uuid" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| const ( | ||
| testRole1 = "New-Admin" | ||
| testRole2 = "Super-Admin" | ||
| testRole3 = "Super Continuous Integration" | ||
| ) | ||
|
|
||
| var ( | ||
| testRoles = set.NewFrozenStringSet(testRole1, testRole2, testRole3) | ||
| ) | ||
|
|
||
| func TestAuthDatastorePostgres(t *testing.T) { | ||
| suite.Run(t, new(datastorePostgresTestSuite)) | ||
| } | ||
|
|
||
| type datastorePostgresTestSuite struct { | ||
| suite.Suite | ||
|
|
||
| ctx context.Context | ||
| pool *pgtest.TestPostgres | ||
| authDataStore DataStore | ||
| roleDataStore roleDataStore.DataStore | ||
| } | ||
|
|
||
| func (s *datastorePostgresTestSuite) SetupTest() { | ||
| s.ctx = sac.WithGlobalAccessScopeChecker(context.Background(), | ||
| sac.AllowFixedScopes( | ||
| sac.AccessModeScopeKeys(storage.Access_READ_ACCESS, storage.Access_READ_WRITE_ACCESS), | ||
| sac.ResourceScopeKeys(resources.Access), | ||
| ), | ||
| ) | ||
msugakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| s.pool = pgtest.ForT(s.T()) | ||
| s.Require().NotNil(s.pool) | ||
|
|
||
| store := pgStore.New(s.pool.DB) | ||
| s.authDataStore = New(store) | ||
|
|
||
| permSetStore := permissionSetPostgresStore.New(s.pool.DB) | ||
| accessScopeStore := accessScopePostgresStore.New(s.pool.DB) | ||
| roleStore := rolePostgresStore.New(s.pool.DB) | ||
| s.roleDataStore = roleDataStore.New(roleStore, permSetStore, accessScopeStore, func(_ context.Context, _ func(*storage.Group) bool) ([]*storage.Group, error) { | ||
| return nil, nil | ||
| }) | ||
|
|
||
| s.addRoles() | ||
|
|
||
| } | ||
|
|
||
| func (s *datastorePostgresTestSuite) TearDownTest() { | ||
| s.pool.Teardown(s.T()) | ||
| s.pool.Close() | ||
| } | ||
|
|
||
| func (s *datastorePostgresTestSuite) TestAddFKConstraint() { | ||
| config, err := s.authDataStore.AddAuthM2MConfig(s.ctx, &storage.AuthMachineToMachineConfig{ | ||
| Id: "80c053c2-24a7-4b97-bd69-85b3a511241e", | ||
| Type: storage.AuthMachineToMachineConfig_GITHUB_ACTIONS, | ||
| TokenExpirationDuration: "5m", | ||
| Mappings: []*storage.AuthMachineToMachineConfig_Mapping{ | ||
| { | ||
| Key: "sub", | ||
| ValueExpression: "some-value", | ||
| Role: "non-existing-role", | ||
| }, | ||
| }, | ||
| }) | ||
| s.ErrorIs(err, errox.ReferencedObjectNotFound) | ||
| s.Nil(config) | ||
| } | ||
|
|
||
| func (s *datastorePostgresTestSuite) TestDeleteFKConstraint() { | ||
| config, err := s.authDataStore.AddAuthM2MConfig(s.ctx, &storage.AuthMachineToMachineConfig{ | ||
| Id: "80c053c2-24a7-4b97-bd69-85b3a511241e", | ||
| Type: storage.AuthMachineToMachineConfig_GITHUB_ACTIONS, | ||
| TokenExpirationDuration: "5m", | ||
| Mappings: []*storage.AuthMachineToMachineConfig_Mapping{ | ||
| { | ||
| Key: "sub", | ||
| ValueExpression: "some-value", | ||
| Role: testRole1, | ||
| }, | ||
| }, | ||
| }) | ||
| s.Require().NoError(err) | ||
|
|
||
| s.ErrorIs(s.roleDataStore.RemoveRole(s.ctx, testRole1), errox.ReferencedByAnotherObject) | ||
|
|
||
| s.NoError(s.authDataStore.RemoveAuthM2MConfig(s.ctx, config.GetId())) | ||
|
|
||
| s.NoError(s.roleDataStore.RemoveRole(s.ctx, testRole1)) | ||
| } | ||
|
|
||
| func (s *datastorePostgresTestSuite) TestAddUniqueIssuerConstraint() { | ||
| _, err := s.authDataStore.AddAuthM2MConfig(s.ctx, &storage.AuthMachineToMachineConfig{ | ||
| Id: "80c053c2-24a7-4b97-bd69-85b3a511241e", | ||
| Type: storage.AuthMachineToMachineConfig_GENERIC, | ||
| TokenExpirationDuration: "5m", | ||
| Mappings: []*storage.AuthMachineToMachineConfig_Mapping{ | ||
| { | ||
| Key: "sub", | ||
| ValueExpression: "some-value", | ||
| Role: testRole1, | ||
| }, | ||
| }, | ||
| Issuer: "https://stackrox.io", | ||
| }) | ||
|
|
||
| s.NoError(err) | ||
|
|
||
| _, err = s.authDataStore.AddAuthM2MConfig(s.ctx, &storage.AuthMachineToMachineConfig{ | ||
| Id: "12c153c2-24a7-4b97-bd69-85b3a511241e", | ||
| Type: storage.AuthMachineToMachineConfig_GENERIC, | ||
| TokenExpirationDuration: "5m", | ||
| Mappings: []*storage.AuthMachineToMachineConfig_Mapping{ | ||
| { | ||
| Key: "sub", | ||
| ValueExpression: "some-value", | ||
| Role: testRole2, | ||
| }, | ||
| }, | ||
| Issuer: "https://stackrox.io", | ||
| }) | ||
|
|
||
| s.Error(err) | ||
| s.ErrorIs(err, errox.AlreadyExists) | ||
| } | ||
|
|
||
| func (s *datastorePostgresTestSuite) addRoles() { | ||
| permSetID := uuid.NewV4().String() | ||
| accessScopeID := uuid.NewV4().String() | ||
| s.Require().NoError(s.roleDataStore.AddPermissionSet(s.ctx, &storage.PermissionSet{ | ||
| Id: permSetID, | ||
| Name: "test permission set", | ||
| Description: "test permission set", | ||
| ResourceToAccess: map[string]storage.Access{ | ||
| resources.Access.String(): storage.Access_READ_ACCESS, | ||
| }, | ||
| })) | ||
| s.Require().NoError(s.roleDataStore.AddAccessScope(s.ctx, &storage.SimpleAccessScope{ | ||
| Id: accessScopeID, | ||
| Name: "test access scope", | ||
| Description: "test access scope", | ||
| Rules: &storage.SimpleAccessScope_Rules{ | ||
| IncludedClusters: []string{"cluster-a"}, | ||
| }, | ||
| })) | ||
|
|
||
| for _, role := range testRoles.AsSlice() { | ||
| s.Require().NoError(s.roleDataStore.AddRole(s.ctx, &storage.Role{ | ||
| Name: role, | ||
| Description: "test role", | ||
| PermissionSetId: permSetID, | ||
| AccessScopeId: accessScopeID, | ||
| })) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package datastore | ||
|
|
||
| import ( | ||
| pgStore "github.com/stackrox/rox/central/auth/store/postgres" | ||
| "github.com/stackrox/rox/central/globaldb" | ||
| "github.com/stackrox/rox/pkg/sync" | ||
| ) | ||
|
|
||
| var ( | ||
| once sync.Once | ||
|
|
||
| ds DataStore | ||
| ) | ||
|
|
||
| // Singleton provides a singleton auth machine to machine DataStore. | ||
| func Singleton() DataStore { | ||
| once.Do(func() { | ||
| ds = New(pgStore.New(globaldb.GetPostgres())) | ||
| }) | ||
| return ds | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.