Skip to content

chore: add provisioner key crud apis #13857

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 20 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests
  • Loading branch information
f0ssel committed Jul 16, 2024
commit 240861221a39b6d13597354dd597ea0f16efe9d2
4 changes: 3 additions & 1 deletion coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6543,9 +6543,11 @@ func (q *FakeQuerier) InsertProvisionerKey(_ context.Context, arg database.Inser
q.mutex.Lock()
defer q.mutex.Unlock()

newErr := *errUniqueConstraint
newErr.Constraint = string(database.UniqueProvisionerKeysOrganizationIDNameKey)
for _, key := range q.provisionerKeys {
if key.ID == arg.ID || (key.OrganizationID == arg.OrganizationID && key.Name == arg.Name) {
return database.ProvisionerKey{}, errUniqueConstraint
return database.ProvisionerKey{}, &newErr
}
}

Expand Down
33 changes: 33 additions & 0 deletions coderd/provisionerkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coderd
import (
"database/sql"
"errors"
"fmt"
"net/http"

"github.com/coder/coder/v2/coderd/database"
Expand All @@ -21,13 +22,45 @@ func (api *API) postProvisionerKey(rw http.ResponseWriter, r *http.Request) {
return
}

if req.Name == "" {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Name is required",
Validations: []codersdk.ValidationError{
{
Field: "name",
Detail: "Name is required",
},
},
})
return
}

if len(req.Name) > 64 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Name must be at most 64 characters",
Validations: []codersdk.ValidationError{
{
Field: "name",
Detail: "Name must be at most 64 characters",
},
},
})
return
}

params, token, err := provisionerkey.New(organization.ID, req.Name)
if err != nil {
httpapi.InternalServerError(rw, err)
return
}

_, err = api.Database.InsertProvisionerKey(ctx, params)
if database.IsUniqueViolation(err, database.UniqueProvisionerKeysOrganizationIDNameKey) {
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
Message: fmt.Sprintf("Provisioner key with name '%s' already exists in organization", req.Name),
})
return
}
if err != nil {
httpapi.InternalServerError(rw, err)
return
Expand Down
14 changes: 4 additions & 10 deletions coderd/provisionerkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,19 @@ func TestProvisionerKeys(t *testing.T) {
_, err = orgAdmin.CreateProvisionerKey(ctx, owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{
Name: "key",
})
require.ErrorContains(t, err, "already exists")

// key name cannot have special characters
_, err = orgAdmin.CreateProvisionerKey(ctx, owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{
Name: "key with spaces",
})
require.ErrorContains(t, err, "org admin create provisioner key")
require.ErrorContains(t, err, "already exists in organization")

// key name cannot be too long
_, err = orgAdmin.CreateProvisionerKey(ctx, owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{
Name: "key with spaces",
Name: "Everyone please pass your watermelons to the front of the pool, the storm is approaching.",
})
require.ErrorContains(t, err, "less than 64 characters")
require.ErrorContains(t, err, "must be at most 64 characters")

// key name cannot be empty
_, err = orgAdmin.CreateProvisionerKey(ctx, owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{
Name: "",
})
require.ErrorContains(t, err, "cannot be empty")
require.ErrorContains(t, err, "is required")

// org admin can list provisioner keys
keys, err = orgAdmin.ListProvisionerKeys(ctx, owner.OrganizationID)
Expand Down
2 changes: 1 addition & 1 deletion codersdk/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ type CreateProvisionerKeyRequest struct {
}

type CreateProvisionerKeyResponse struct {
Key string
Key string `json:"key"`
}

// CreateProvisionerKey creates a new provisioner key for an organization.
Expand Down