-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upsert provider access tokens instead of Create and Delete (#2486)
For OAuth flow provider enrollments we used to delete and recreate the provider access token in order to be able to list the new token based on CreatedAt. For the case where we pass a PAT, we would just error out in case the token was already created. Let's just upsert in both cases and instead of CreatedAt let's look at UpdatedAt. This will be useful for tests. Fixes: #2411
- Loading branch information
Showing
6 changed files
with
126 additions
and
133 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,17 +1,22 @@ | ||
-- name: CreateAccessToken :one | ||
INSERT INTO provider_access_tokens (project_id, provider, encrypted_token, expiration_time, owner_filter) VALUES ($1, $2, $3, $4, $5) RETURNING *; | ||
|
||
-- name: GetAccessTokenByProjectID :one | ||
SELECT * FROM provider_access_tokens WHERE provider = $1 AND project_id = $2; | ||
|
||
-- name: UpdateAccessToken :one | ||
UPDATE provider_access_tokens SET encrypted_token = $3, expiration_time = $4, owner_filter = $5, updated_at = NOW() WHERE provider = $1 AND project_id = $2 RETURNING *; | ||
|
||
-- name: DeleteAccessToken :exec | ||
DELETE FROM provider_access_tokens WHERE provider = $1 AND project_id = $2; | ||
|
||
-- name: GetAccessTokenByProvider :many | ||
SELECT * FROM provider_access_tokens WHERE provider = $1; | ||
|
||
-- name: GetAccessTokenSinceDate :one | ||
SELECT * FROM provider_access_tokens WHERE provider = $1 AND project_id = $2 AND created_at >= $3; | ||
SELECT * FROM provider_access_tokens WHERE provider = $1 AND project_id = $2 AND updated_at >= $3; | ||
|
||
-- name: UpsertAccessToken :one | ||
INSERT INTO provider_access_tokens | ||
(project_id, provider, encrypted_token, expiration_time, owner_filter) | ||
VALUES | ||
($1, $2, $3, $4, $5) | ||
ON CONFLICT (project_id, provider) | ||
DO UPDATE SET | ||
encrypted_token = $3, | ||
expiration_time = $4, | ||
owner_filter = $5, | ||
updated_at = NOW() | ||
WHERE provider_access_tokens.project_id = $1 AND provider_access_tokens.provider = $2 | ||
RETURNING *; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,65 @@ | ||
// | ||
// Copyright 2023 Stacklok, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package db | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUpsertProviderAccessToken(t *testing.T) { | ||
t.Parallel() | ||
|
||
org := createRandomOrganization(t) | ||
project := createRandomProject(t, org.ID) | ||
prov := createRandomProvider(t, project.ID) | ||
|
||
tok, err := testQueries.UpsertAccessToken(context.Background(), UpsertAccessTokenParams{ | ||
ProjectID: project.ID, | ||
Provider: prov.Name, | ||
EncryptedToken: "abc", | ||
OwnerFilter: sql.NullString{}, | ||
}) | ||
|
||
require.NoError(t, err) | ||
require.NotEmpty(t, tok) | ||
require.NotEmpty(t, tok.ID) | ||
require.NotEmpty(t, tok.CreatedAt) | ||
require.NotEmpty(t, tok.UpdatedAt) | ||
require.Equal(t, project.ID, tok.ProjectID) | ||
require.Equal(t, prov.Name, tok.Provider) | ||
require.Equal(t, "abc", tok.EncryptedToken) | ||
require.Equal(t, sql.NullString{}, tok.OwnerFilter) | ||
|
||
tokUpdate, err := testQueries.UpsertAccessToken(context.Background(), UpsertAccessTokenParams{ | ||
ProjectID: project.ID, | ||
Provider: prov.Name, | ||
EncryptedToken: "def", | ||
OwnerFilter: sql.NullString{}, | ||
}) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, project.ID, tokUpdate.ProjectID) | ||
require.Equal(t, prov.Name, tokUpdate.Provider) | ||
require.Equal(t, "def", tokUpdate.EncryptedToken) | ||
require.Equal(t, sql.NullString{}, tokUpdate.OwnerFilter) | ||
require.Equal(t, tok.ID, tokUpdate.ID) | ||
require.Equal(t, tok.CreatedAt, tokUpdate.CreatedAt) | ||
require.NotEqual(t, tok.UpdatedAt, tokUpdate.UpdatedAt) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.