-
Notifications
You must be signed in to change notification settings - Fork 40
Move github credentials to the database #243
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
gabriel-samfira
merged 27 commits into
cloudbase:main
from
gabriel-samfira:use-db-for-gh-creds
May 9, 2024
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
90870c1
Use database for github creds
gabriel-samfira 032d40f
Fix tests
gabriel-samfira 3e60a48
Preload credentials endpoint and remove extra code
gabriel-samfira 9c1ffe8
Enforce same endpoint when updating credentials
gabriel-samfira 4610f83
List credentials from db
gabriel-samfira 257fb0b
Take into account legacy config
gabriel-samfira 77ecb16
Add github endpoint API endpoint and CLI code
gabriel-samfira eadbe78
Add github credentials API and cli code
gabriel-samfira 208a4ee
Ensure github endpoint
gabriel-samfira eb14564
Deny deleting the default github.com endpoint
gabriel-samfira e8ea711
Fix tests post-rebase
gabriel-samfira 8b5584f
Add some e2e boilerplate
gabriel-samfira f5682e6
Work around actions/runner-images#7210
gabriel-samfira c2b974d
Create clone creds for repo update
gabriel-samfira 1256473
Fetch credentials from DB
gabriel-samfira ccf5105
Add github endpoint operations e2e test
gabriel-samfira 39a5e14
Add more e2e GH endpoint tests
gabriel-samfira 0128f59
Add some credentials e2e tests
gabriel-samfira 87943db
Fix lint and add integration tests logging
gabriel-samfira 349ba1f
Fix nil pointer dereference
gabriel-samfira 402c8b7
Use different creds than the default ones
gabriel-samfira 2b1414d
Add some unit tests
gabriel-samfira 2a3d524
Add more unit tests
gabriel-samfira 8726cb9
Move the name check before tx
gabriel-samfira 75eb45c
Update database/sql/sql.go
gabriel-samfira 27e74ef
Add DB migration test
gabriel-samfira b3e2c58
Update docs
gabriel-samfira 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "log/slog" | ||
| "math" | ||
| "net/http" | ||
| "strconv" | ||
|
|
||
| "github.com/gorilla/mux" | ||
|
|
||
| gErrors "github.com/cloudbase/garm-provider-common/errors" | ||
| "github.com/cloudbase/garm/params" | ||
| ) | ||
|
|
||
| // swagger:route GET /credentials credentials ListCredentials | ||
| // swagger:route GET /github/credentials credentials ListCredentials | ||
| // | ||
| // List all credentials. | ||
| // | ||
| // Responses: | ||
| // 200: Credentials | ||
| // 400: APIErrorResponse | ||
| func (a *APIController) ListCredentials(w http.ResponseWriter, r *http.Request) { | ||
| ctx := r.Context() | ||
| creds, err := a.r.ListCredentials(ctx) | ||
| if err != nil { | ||
| handleError(ctx, w, err) | ||
| return | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| if err := json.NewEncoder(w).Encode(creds); err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") | ||
| } | ||
| } | ||
|
|
||
| // swagger:route POST /github/credentials credentials CreateCredentials | ||
| // | ||
| // Create a GitHub credential. | ||
| // | ||
| // Parameters: | ||
| // + name: Body | ||
| // description: Parameters used when creating a GitHub credential. | ||
| // type: CreateGithubCredentialsParams | ||
| // in: body | ||
| // required: true | ||
| // | ||
| // Responses: | ||
| // 200: GithubCredentials | ||
| // 400: APIErrorResponse | ||
| func (a *APIController) CreateGithubCredential(w http.ResponseWriter, r *http.Request) { | ||
| ctx := r.Context() | ||
|
|
||
| var params params.CreateGithubCredentialsParams | ||
| if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to decode request") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| cred, err := a.r.CreateGithubCredentials(ctx, params) | ||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to create GitHub credential") | ||
| handleError(ctx, w, err) | ||
| return | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if err := json.NewEncoder(w).Encode(cred); err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") | ||
| } | ||
| } | ||
|
|
||
| // swagger:route GET /github/credentials/{id} credentials GetCredentials | ||
| // | ||
| // Get a GitHub credential. | ||
| // | ||
| // Parameters: | ||
| // + name: id | ||
| // description: ID of the GitHub credential. | ||
| // type: integer | ||
| // in: path | ||
| // required: true | ||
| // | ||
| // Responses: | ||
| // 200: GithubCredentials | ||
| // 400: APIErrorResponse | ||
| func (a *APIController) GetGithubCredential(w http.ResponseWriter, r *http.Request) { | ||
| ctx := r.Context() | ||
| vars := mux.Vars(r) | ||
| idParam, ok := vars["id"] | ||
| if !ok { | ||
| slog.ErrorContext(ctx, "missing id in request") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| id, err := strconv.ParseUint(idParam, 10, 64) | ||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to parse id") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| if id > math.MaxUint { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "id is too large") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| cred, err := a.r.GetGithubCredentials(ctx, uint(id)) | ||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to get GitHub credential") | ||
| handleError(ctx, w, err) | ||
| return | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| if err := json.NewEncoder(w).Encode(cred); err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") | ||
| } | ||
| } | ||
|
|
||
| // swagger:route DELETE /github/credentials/{id} credentials DeleteCredentials | ||
| // | ||
| // Delete a GitHub credential. | ||
| // | ||
| // Parameters: | ||
| // + name: id | ||
| // description: ID of the GitHub credential. | ||
| // type: integer | ||
| // in: path | ||
| // required: true | ||
| // | ||
| // Responses: | ||
| // default: APIErrorResponse | ||
| func (a *APIController) DeleteGithubCredential(w http.ResponseWriter, r *http.Request) { | ||
| ctx := r.Context() | ||
| vars := mux.Vars(r) | ||
| idParam, ok := vars["id"] | ||
| if !ok { | ||
| slog.ErrorContext(ctx, "missing id in request") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| id, err := strconv.ParseUint(idParam, 10, 64) | ||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to parse id") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| if id > math.MaxUint { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "id is too large") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| if err := a.r.DeleteGithubCredentials(ctx, uint(id)); err != nil { | ||
|
||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to delete GitHub credential") | ||
| handleError(ctx, w, err) | ||
| return | ||
| } | ||
|
|
||
| w.WriteHeader(http.StatusNoContent) | ||
| } | ||
|
|
||
| // swagger:route PUT /github/credentials/{id} credentials UpdateCredentials | ||
| // | ||
| // Update a GitHub credential. | ||
| // | ||
| // Parameters: | ||
| // + name: id | ||
| // description: ID of the GitHub credential. | ||
| // type: integer | ||
| // in: path | ||
| // required: true | ||
| // + name: Body | ||
| // description: Parameters used when updating a GitHub credential. | ||
| // type: UpdateGithubCredentialsParams | ||
| // in: body | ||
| // required: true | ||
| // | ||
| // Responses: | ||
| // 200: GithubCredentials | ||
| // 400: APIErrorResponse | ||
| func (a *APIController) UpdateGithubCredential(w http.ResponseWriter, r *http.Request) { | ||
| ctx := r.Context() | ||
| vars := mux.Vars(r) | ||
| idParam, ok := vars["id"] | ||
| if !ok { | ||
| slog.ErrorContext(ctx, "missing id in request") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| id, err := strconv.ParseUint(idParam, 10, 64) | ||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to parse id") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| if id > math.MaxUint { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "id is too large") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| var params params.UpdateGithubCredentialsParams | ||
| if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to decode request") | ||
| handleError(ctx, w, gErrors.ErrBadRequest) | ||
| return | ||
| } | ||
|
|
||
| cred, err := a.r.UpdateGithubCredentials(ctx, uint(id), params) | ||
|
||
| if err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to update GitHub credential") | ||
| handleError(ctx, w, err) | ||
| return | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| if err := json.NewEncoder(w).Encode(cred); err != nil { | ||
| slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response") | ||
| } | ||
| } | ||
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.