Skip to content

feat(coderd): add endpoint to fetch provisioner key details #15505

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 13 commits into from
Nov 20, 2024
Prev Previous commit
Next Next commit
work on tests
  • Loading branch information
defelmnq committed Nov 20, 2024
commit 0af9e8e572e6a2cd53dbee7f31945ea2837e23c4
61 changes: 60 additions & 1 deletion enterprise/coderd/provisionerkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func TestGetProvisionerKey(t *testing.T) {
}{
{
name: "ok",
useFakeKey: false,
success: true,
expectedErr: "",
},
Expand Down Expand Up @@ -186,6 +185,7 @@ func TestGetProvisionerKey(t *testing.T) {
},
})

//nolint:gocritic // ignore This client is operating as the owner user, which has unrestricted permissions
key, err := client.CreateProvisionerKey(ctx, owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{
Name: "my-test-key",
Tags: map[string]string{"key1": "value1", "key2": "value2"},
Expand All @@ -207,4 +207,63 @@ func TestGetProvisionerKey(t *testing.T) {
}
})
}

t.Run("TestPSK", func(t *testing.T) {
t.Parallel()

ctx := testutil.Context(t, testutil.WaitShort)
dv := coderdtest.DeploymentValues(t)
client, owner := coderdenttest.New(t, &coderdenttest.Options{
ProvisionerDaemonPSK: "psk-testing-purpose",
Options: &coderdtest.Options{
DeploymentValues: dv,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureMultipleOrganizations: 1,
codersdk.FeatureExternalProvisionerDaemons: 1,
},
},
})

//nolint:gocritic // ignore This client is operating as the owner user, which has unrestricted permissions
_, err := client.CreateProvisionerKey(ctx, owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{
Name: "my-test-key",
Tags: map[string]string{"key1": "value1", "key2": "value2"},
})
require.NoError(t, err)

fetchedKey, err := client.GetProvisionerKey(ctx, "psk-testing-purpose")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use a const here

require.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be quite specific in the error you're checking for. The intent is not expressed clearly enough here.
This test should effectively be saying "Provisioner PSKs are similar to provisioner keys, but they cannot be used to retrieve information about a provisioner, so we get this very specific error message".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I feel like i'm maybe missing something or we're over testing the same thing here. as the code is always coding the same method and filling the same header - we will always fall into the same test case in the middleware.

Not that I want to minimise the tests - but at the end :
https://github.com/coder/coder/pull/15505/files#diff-2eae6501c14fe87b5578bcd6fda3588cffb28d437c61765e3829ff530906927cR376 -> here we set the same header hardcoded.
https://github.com/coder/coder/blob/main/coderd/httpmw/provisionerdaemon.go#L28 -> This middleware will never really go into the PSK / CoderSession logic as they're never set on the other side.

I would rather add tests into the middleware if we want to increase coverage.

require.Empty(t, fetchedKey)
})

t.Run("TestSessionToken", func(t *testing.T) {
t.Parallel()

ctx := testutil.Context(t, testutil.WaitShort)
dv := coderdtest.DeploymentValues(t)
client, owner := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
DeploymentValues: dv,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureMultipleOrganizations: 1,
codersdk.FeatureExternalProvisionerDaemons: 1,
},
},
})

//nolint:gocritic // ignore This client is operating as the owner user, which has unrestricted permissions
_, err := client.CreateProvisionerKey(ctx, owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{
Name: "my-test-key",
Tags: map[string]string{"key1": "value1", "key2": "value2"},
})
require.NoError(t, err)

fetchedKey, err := client.GetProvisionerKey(ctx, client.SessionToken())
require.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

require.Empty(t, fetchedKey)
})
}
Loading