Skip to content
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

add import ssh key for specific project #1487

Merged
merged 4 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 26 additions & 1 deletion modules/gcp/oslogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ func ImportSSHKey(t testing.TestingT, user, key string) {
// The `user` parameter should be the email address of the user.
// The `key` parameter should be the public key of the SSH key being uploaded.
func ImportSSHKeyE(t testing.TestingT, user, key string) error {
return importProjectSSHKeyE(t, user, key, nil)
}

// ImportProjectSSHKey will import an SSH key to GCP under the provided user identity.
// The `user` parameter should be the email address of the user.
// The `key` parameter should be the public key of the SSH key being uploaded.
// The `projectID` parameter should be the chosen project ID.
// This will fail the test if there is an error.
func ImportProjectSSHKey(t testing.TestingT, user, key, projectID string) {
require.NoErrorf(t, ImportProjectSSHKeyE(t, user, key, projectID), "Could not add SSH Key to user %s", user)
}

// ImportProjectSSHKeyE will import an SSH key to GCP under the provided user identity.
// The `user` parameter should be the email address of the user.
// The `key` parameter should be the public key of the SSH key being uploaded.
// The `projectID` parameter should be the chosen project ID.
func ImportProjectSSHKeyE(t testing.TestingT, user, key, projectID string) error {
return importProjectSSHKeyE(t, user, key, &projectID)
}

func importProjectSSHKeyE(t testing.TestingT, user, key string, projectID *string) error {
logger.Default.Logf(t, "Importing SSH key for user %s", user)

ctx := context.Background()
Expand All @@ -38,7 +59,11 @@ func ImportSSHKeyE(t testing.TestingT, user, key string) error {
Key: key,
}

_, err = service.Users.ImportSshPublicKey(parent, sshPublicKey).Context(ctx).Do()
req := service.Users.ImportSshPublicKey(parent, sshPublicKey)
if projectID != nil {
req = req.ProjectId(*projectID)
}
_, err = req.Context(ctx).Do()
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions modules/gcp/oslogin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ func TestImportSSHKeyOSLogin(t *testing.T) {
ImportSSHKey(t, user, key)
}

func TestImportProjectSSHKeyOSLogin(t *testing.T) {
t.Parallel()

keyPair := ssh.GenerateRSAKeyPair(t, 2048)
key := keyPair.PublicKey

user := GetGoogleIdentityEmailEnvVar(t)
projectID := GetGoogleProjectIDFromEnvVar(t)

defer DeleteSSHKey(t, user, key)
ImportProjectSSHKey(t, user, key, projectID)
}

func TestGetLoginProfile(t *testing.T) {
t.Parallel()

Expand Down