-
Notifications
You must be signed in to change notification settings - Fork 38
feat(RBAC): enable project-scoped API tokens #2143
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
15 commits
Select commit
Hold shift + click to select a range
d07e5b5
change data model
migmartri c5e55d5
update token
migmartri ab5b3f7
update token
migmartri 69ed110
chore: list
migmartri 720b151
chore: list
migmartri 2c13550
chore
migmartri ce1a465
fix test
migmartri cf7cc4a
fix test
migmartri 4ba9e63
fix test
migmartri e720034
chore: generate token
migmartri 6f003c6
feat: bundle authorization
migmartri 83f2bb0
feat: bundle authorization
migmartri af6bb4c
rebase
migmartri 34cebb9
fix tests
migmartri 2b9a44b
fix tests
migmartri 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
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
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
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 |
---|---|---|
|
@@ -128,15 +128,15 @@ func NewAPITokenUseCase(apiTokenRepo APITokenRepo, jwtConfig *APITokenJWTConfig, | |
} | ||
|
||
type apiTokenOptions struct { | ||
projectID *uuid.UUID | ||
project *Project | ||
showOnlySystemTokens bool | ||
} | ||
|
||
type APITokenUseCaseOpt func(*apiTokenOptions) | ||
|
||
func APITokenWithProjectID(projectID uuid.UUID) APITokenUseCaseOpt { | ||
func APITokenWithProject(project *Project) APITokenUseCaseOpt { | ||
return func(o *apiTokenOptions) { | ||
o.projectID = &projectID | ||
o.project = project | ||
} | ||
} | ||
|
||
|
@@ -181,18 +181,38 @@ func (uc *APITokenUseCase) Create(ctx context.Context, name string, description | |
return nil, fmt.Errorf("finding organization: %w", err) | ||
} | ||
|
||
// If a project is provided, we store it in the token | ||
var projectID *uuid.UUID | ||
if options.project != nil { | ||
projectID = ToPtr(options.project.ID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does, I was just using one way of doing it tbh, a little bit pointless it's true |
||
} | ||
|
||
// NOTE: the expiration time is stored just for reference, it's also encoded in the JWT | ||
// We store it since Chainloop will not have access to the JWT to check the expiration once created | ||
token, err := uc.apiTokenRepo.Create(ctx, name, description, expiresAt, orgUUID, options.projectID) | ||
migmartri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
token, err := uc.apiTokenRepo.Create(ctx, name, description, expiresAt, orgUUID, projectID) | ||
if err != nil { | ||
if IsErrAlreadyExists(err) { | ||
return nil, NewErrAlreadyExistsStr("name already taken") | ||
} | ||
return nil, fmt.Errorf("storing token: %w", err) | ||
} | ||
|
||
generationOpts := &apitoken.GenerateJWTOptions{ | ||
OrgID: token.OrganizationID, | ||
OrgName: org.Name, | ||
KeyID: token.ID, | ||
KeyName: name, | ||
ExpiresAt: expiresAt, | ||
} | ||
|
||
if projectID != nil { | ||
generationOpts.ProjectID = ToPtr(options.project.ID) | ||
generationOpts.ProjectName = ToPtr(options.project.Name) | ||
} | ||
|
||
// generate the JWT | ||
token.JWT, err = uc.jwtBuilder.GenerateJWT(token.OrganizationID.String(), org.Name, token.ID.String(), expiresAt) | ||
token.JWT, err = uc.jwtBuilder.GenerateJWT(generationOpts) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("generating jwt: %w", err) | ||
} | ||
|
@@ -233,8 +253,16 @@ func (uc *APITokenUseCase) RegenerateJWT(ctx context.Context, tokenID uuid.UUID, | |
return nil, fmt.Errorf("finding organization: %w", err) | ||
} | ||
|
||
generationOpts := &apitoken.GenerateJWTOptions{ | ||
OrgID: token.OrganizationID, | ||
OrgName: org.Name, | ||
KeyID: token.ID, | ||
KeyName: token.Name, | ||
ExpiresAt: &expiresAt, | ||
} | ||
|
||
// generate the JWT | ||
token.JWT, err = uc.jwtBuilder.GenerateJWT(token.OrganizationID.String(), org.Name, token.ID.String(), &expiresAt) | ||
token.JWT, err = uc.jwtBuilder.GenerateJWT(generationOpts) | ||
if err != nil { | ||
return nil, fmt.Errorf("generating jwt: %w", err) | ||
} | ||
|
@@ -258,7 +286,12 @@ func (uc *APITokenUseCase) List(ctx context.Context, orgID string, includeRevoke | |
return nil, NewErrInvalidUUID(err) | ||
} | ||
|
||
return uc.apiTokenRepo.List(ctx, &orgUUID, options.projectID, includeRevoked, options.showOnlySystemTokens) | ||
var projectID *uuid.UUID | ||
if options.project != nil { | ||
projectID = ToPtr(options.project.ID) | ||
} | ||
|
||
return uc.apiTokenRepo.List(ctx, &orgUUID, projectID, includeRevoked, options.showOnlySystemTokens) | ||
} | ||
|
||
func (uc *APITokenUseCase) Revoke(ctx context.Context, orgID, id string) error { | ||
|
@@ -308,7 +341,12 @@ func (uc *APITokenUseCase) FindByNameInOrg(ctx context.Context, orgID, name stri | |
return nil, NewErrInvalidUUID(err) | ||
} | ||
|
||
t, err := uc.apiTokenRepo.FindByNameInOrg(ctx, orgUUID, name, options.projectID) | ||
var projectID *uuid.UUID | ||
if options.project != nil { | ||
projectID = ToPtr(options.project.ID) | ||
} | ||
|
||
t, err := uc.apiTokenRepo.FindByNameInOrg(ctx, orgUUID, name, projectID) | ||
if err != nil { | ||
return nil, fmt.Errorf("finding token: %w", err) | ||
} | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the token has a ProjectID, is this change checking that the ProjectID in the database is the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mm, it is not, let me add it
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I was using the projectID from the DB when in reality we need to use the one from the token.
Now I check that both match