|
| 1 | +package coderd_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/coder/coder/v2/coderd/coderdtest" |
| 11 | + "github.com/coder/coder/v2/coderd/rbac" |
| 12 | + "github.com/coder/coder/v2/codersdk" |
| 13 | + "github.com/coder/coder/v2/codersdk/agentsdk" |
| 14 | + "github.com/coder/coder/v2/enterprise/coderd/coderdenttest" |
| 15 | + "github.com/coder/coder/v2/enterprise/coderd/license" |
| 16 | + "github.com/coder/coder/v2/provisioner/echo" |
| 17 | + "github.com/coder/coder/v2/testutil" |
| 18 | +) |
| 19 | + |
| 20 | +// TestAgentGitSSHKeyCustomRoles tests that the agent can fetch its git ssh key when |
| 21 | +// the user has a custom role in a second workspace. |
| 22 | +func TestAgentGitSSHKeyCustomRoles(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + owner, _ := coderdenttest.New(t, &coderdenttest.Options{ |
| 26 | + Options: &coderdtest.Options{ |
| 27 | + IncludeProvisionerDaemon: true, |
| 28 | + }, |
| 29 | + LicenseOptions: &coderdenttest.LicenseOptions{ |
| 30 | + Features: license.Features{ |
| 31 | + codersdk.FeatureCustomRoles: 1, |
| 32 | + codersdk.FeatureMultipleOrganizations: 1, |
| 33 | + codersdk.FeatureExternalProvisionerDaemons: 1, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }) |
| 37 | + |
| 38 | + // When custom roles exist in a second organization |
| 39 | + org := coderdenttest.CreateOrganization(t, owner, coderdenttest.CreateOrganizationOptions{ |
| 40 | + IncludeProvisionerDaemon: true, |
| 41 | + }) |
| 42 | + |
| 43 | + ctx := testutil.Context(t, testutil.WaitShort) |
| 44 | + //nolint:gocritic // required to make orgs |
| 45 | + newRole, err := owner.CreateOrganizationRole(ctx, codersdk.Role{ |
| 46 | + Name: "custom", |
| 47 | + OrganizationID: org.ID.String(), |
| 48 | + DisplayName: "", |
| 49 | + SitePermissions: nil, |
| 50 | + OrganizationPermissions: codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ |
| 51 | + codersdk.ResourceTemplate: {codersdk.ActionRead, codersdk.ActionCreate, codersdk.ActionUpdate}, |
| 52 | + }), |
| 53 | + UserPermissions: nil, |
| 54 | + }) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + // Create the new user |
| 58 | + client, _ := coderdtest.CreateAnotherUser(t, owner, org.ID, rbac.RoleIdentifier{Name: newRole.Name, OrganizationID: org.ID}) |
| 59 | + |
| 60 | + // Create the workspace + agent |
| 61 | + authToken := uuid.NewString() |
| 62 | + version := coderdtest.CreateTemplateVersion(t, client, org.ID, &echo.Responses{ |
| 63 | + Parse: echo.ParseComplete, |
| 64 | + ProvisionPlan: echo.PlanComplete, |
| 65 | + ProvisionApply: echo.ProvisionApplyWithAgent(authToken), |
| 66 | + }) |
| 67 | + project := coderdtest.CreateTemplate(t, client, org.ID, version.ID) |
| 68 | + coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) |
| 69 | + workspace := coderdtest.CreateWorkspace(t, client, project.ID) |
| 70 | + coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID) |
| 71 | + |
| 72 | + agentClient := agentsdk.New(client.URL) |
| 73 | + agentClient.SetSessionToken(authToken) |
| 74 | + |
| 75 | + ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) |
| 76 | + defer cancel() |
| 77 | + |
| 78 | + agentKey, err := agentClient.GitSSHKey(ctx) |
| 79 | + require.NoError(t, err) |
| 80 | + require.NotEmpty(t, agentKey.PrivateKey) |
| 81 | +} |
0 commit comments