Skip to content

Commit

Permalink
Merge branch 'main' into dont-merge-stderr-to-stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
g7r authored Jan 6, 2025
2 parents df82e51 + a746562 commit 75535df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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

0 comments on commit 75535df

Please sign in to comment.