-
Notifications
You must be signed in to change notification settings - Fork 37
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
Feat: Add a public token type that has only READ access to calling deployments #647
Open
jsun-m
wants to merge
6
commits into
main
Choose a base branch
from
jm/add-workspace-public-token-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+481
−255
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a385d9f
Certain token type
jsun-m 4d1c2e5
Add tokentype to token creation
jsun-m 2e58362
Add low permission public token for workspace
jsun-m 9c44037
Rename token to 'public'
jsun-m beb0293
Update var name
jsun-m ad8d6cf
Merged
jsun-m 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 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 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 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,70 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGRPCValidateToken(t *testing.T) { | ||
mockDetails := mockBackendWithValidToken() | ||
|
||
tests := []struct { | ||
name string | ||
tokenKey string | ||
prepares func() | ||
success bool | ||
}{ | ||
{ | ||
name: "valid token", | ||
tokenKey: mockDetails.tokenForTest.Key, | ||
prepares: func() { | ||
mockDetails.mockRedis.FlushAll(context.Background()) | ||
addTokenRow(mockDetails.mock, *mockDetails.tokenForTest.Workspace, mockDetails.tokenForTest) | ||
}, | ||
success: true, | ||
}, | ||
{ | ||
name: "invalid token", | ||
tokenKey: mockDetails.tokenForTest.Key, | ||
prepares: func() { | ||
mockDetails.mockRedis.FlushAll(context.Background()) | ||
}, | ||
success: false, | ||
}, | ||
{ | ||
name: "no token", | ||
tokenKey: "", | ||
prepares: func() {}, | ||
success: false, | ||
}, | ||
{ | ||
name: "workspace public token should fail", | ||
tokenKey: mockDetails.publicTokenForTest.Key, | ||
prepares: func() { | ||
mockDetails.mockRedis.FlushAll(context.Background()) | ||
addTokenRow(mockDetails.mock, *mockDetails.publicTokenForTest.Workspace, mockDetails.publicTokenForTest) | ||
}, | ||
success: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.prepares() | ||
ai := NewAuthInterceptor(mockDetails.backendRepo, mockDetails.workspaceRepo) | ||
|
||
md := map[string][]string{ | ||
"authorization": {tt.tokenKey}, | ||
} | ||
authInfo, ok := ai.validateToken(md) | ||
|
||
assert.Equal(t, tt.success, ok) | ||
if tt.success { | ||
assert.Equal(t, mockDetails.tokenForTest.Key, authInfo.Token.Key) | ||
assert.Equal(t, mockDetails.tokenForTest.Workspace.ExternalId, authInfo.Workspace.ExternalId) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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 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 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
44 changes: 44 additions & 0 deletions
44
pkg/repository/backend_postgres_migrations/017_add_public_token_type.go
This file contains 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,44 @@ | ||
package backend_postgres_migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/pressly/goose/v3" | ||
) | ||
|
||
func init() { | ||
goose.AddMigrationContext(upAddPublicTokenType, downAddPublicTokenType) | ||
} | ||
|
||
func upAddPublicTokenType(ctx context.Context, tx *sql.Tx) error { | ||
newTokenTypes := []string{"public"} | ||
|
||
for _, tokenType := range newTokenTypes { | ||
addEnumSQL := fmt.Sprintf(` | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS ( | ||
SELECT 1 FROM pg_type t | ||
JOIN pg_enum e ON t.oid = e.enumtypid | ||
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace | ||
WHERE n.nspname = 'public' AND t.typname = 'token_type' AND e.enumlabel = '%s' | ||
) THEN | ||
EXECUTE 'ALTER TYPE token_type ADD VALUE ' || quote_literal('%s'); | ||
END IF; | ||
END | ||
$$;`, tokenType, tokenType) | ||
|
||
if _, err := tx.Exec(addEnumSQL); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func downAddPublicTokenType(ctx context.Context, tx *sql.Tx) error { | ||
// No need to revert this migration | ||
return nil | ||
} |
This file contains 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.
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.
what should we name it?
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.
I think
TokenTypeReadOnly
because its being used as just that. If we decide to have permissions on tokens, then having a "Public" token is redundant with a "Workspace" token. So this feels more like a temporary feature to me until we have something more elaborate.If we want to get more specific,
TokenTypeDeploymentReadOnly
. This creates room for other resource types like tasks or volumes, but it could make the auth closure logic more complicated to follow, challenging to add more token types, and difficult to maintain.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.
what about
TokenTypeRequestOnly
?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.
or
TokenTypeCallAPIOnly
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.
I think those are less clear. Neither "Request" nor "CallAPIOnly" indicate to me that this is a read-only token. In both names, one could still assume they have write permissions.
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.
TokenTypeDeploymentReadOnly
sounds like they can list deployments as well. I can't think of the perfect name for this.what about
TokenTypeAPIRestricted