Skip to content

Commit 60716fe

Browse files
committed
Add oslogin_ssh_key_expire_after parameter for OS Login SSH key expiration
- Add OSLoginSSHKeyExpireAfter field to Config struct - Update ImportOSLoginSSHKey to accept expiration time parameter - Set ExpirationTimeUsec in oslogin.SshPublicKey when provided - Parameter is positioned next to other OS Login configs (oslogin_ssh_username) - Regenerate config.hcl2spec.go with make generate
1 parent 521bf1e commit 60716fe

8 files changed

Lines changed: 39 additions & 6 deletions

File tree

.web-docs/components/builder/googlecompute/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ builder.
372372

373373
Note: Invalid or unsupported values will result in an error during provisioning.
374374

375+
- `oslogin_ssh_key_expire_after` (duration string | ex: "1h5m2s") - The duration after which an SSH key imported into OS Login will expire.
376+
This parameter is used when an SSH key is imported into the OS Login profile.
377+
The expiration time is calculated from the current time plus this duration.
378+
Example value: `1h`, `30m`, `24h`.
379+
375380
- `wait_to_add_ssh_keys` (duration string | ex: "1h5m2s") - The time to wait between the creation of the instance used to create the image,
376381
and the addition of SSH configuration, including SSH keys, to that instance.
377382
The delay is intended to protect packer from anything in the instance boot

builder/googlecompute/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ type Config struct {
369369
//
370370
// Note: Invalid or unsupported values will result in an error during provisioning.
371371
OSLoginSSHUsername string `mapstructure:"oslogin_ssh_username" required:"false"`
372+
// The duration after which an SSH key imported into OS Login will expire.
373+
// This parameter is used when an SSH key is imported into the OS Login profile.
374+
// The expiration time is calculated from the current time plus this duration.
375+
// Example value: `1h`, `30m`, `24h`.
376+
OSLoginSSHKeyExpireAfter time.Duration `mapstructure:"oslogin_ssh_key_expire_after" required:"false"`
372377
// The time to wait between the creation of the instance used to create the image,
373378
// and the addition of SSH configuration, including SSH keys, to that instance.
374379
// The delay is intended to protect packer from anything in the instance boot

builder/googlecompute/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/googlecompute/step_import_os_login_ssh_key.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,16 @@ func (s *StepImportOSLoginSSHKey) Run(ctx context.Context, state multistep.State
102102
return multistep.ActionHalt
103103
}
104104

105-
loginProfile, err := driver.ImportOSLoginSSHKey(s.accountEmail, string(config.Comm.SSHPublicKey))
105+
// Calculate expiration time if oslogin_ssh_key_expire_after is set
106+
// This is used when an SSH key is imported into the OS Login profile
107+
var expirationTimeUsec *int64
108+
if config.OSLoginSSHKeyExpireAfter > 0 {
109+
expirationTime := time.Now().Add(config.OSLoginSSHKeyExpireAfter)
110+
expirationTimeUsecVal := expirationTime.UnixMicro()
111+
expirationTimeUsec = &expirationTimeUsecVal
112+
}
113+
114+
loginProfile, err := driver.ImportOSLoginSSHKey(s.accountEmail, string(config.Comm.SSHPublicKey), expirationTimeUsec)
106115
if err != nil {
107116
err := fmt.Errorf("Error importing SSH public key for OSLogin: %s", err)
108117
state.Put("error", err)

docs-partials/builder/googlecompute/Config-not-required.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@
318318

319319
Note: Invalid or unsupported values will result in an error during provisioning.
320320

321+
- `oslogin_ssh_key_expire_after` (duration string | ex: "1h5m2s") - The duration after which an SSH key imported into OS Login will expire.
322+
This parameter is used when an SSH key is imported into the OS Login profile.
323+
The expiration time is calculated from the current time plus this duration.
324+
Example value: `1h`, `30m`, `24h`.
325+
321326
- `wait_to_add_ssh_keys` (duration string | ex: "1h5m2s") - The time to wait between the creation of the instance used to create the image,
322327
and the addition of SSH configuration, including SSH keys, to that instance.
323328
The delay is intended to protect packer from anything in the instance boot

lib/common/driver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ type Driver interface {
8686
CreateOrResetWindowsPassword(zone, name string, config *WindowsPasswordConfig) (<-chan error, error)
8787

8888
// ImportOSLoginSSHKey imports SSH public key for OSLogin.
89-
ImportOSLoginSSHKey(user, sshPublicKey string) (*oslogin.LoginProfile, error)
89+
// expirationTimeUsec is an optional expiration time in microseconds since Unix epoch.
90+
// If nil, no expiration time will be set.
91+
ImportOSLoginSSHKey(user, sshPublicKey string, expirationTimeUsec *int64) (*oslogin.LoginProfile, error)
9092

9193
// DeleteOSLoginSSHKey deletes the SSH public key for OSLogin with the given key.
9294
DeleteOSLoginSSHKey(user, fingerprint string) error

lib/common/driver_gce.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,12 +898,17 @@ func (d *driverGCE) getPasswordResponses(zone, instance string) ([]windowsPasswo
898898
return passwordResponses, nil
899899
}
900900

901-
func (d *driverGCE) ImportOSLoginSSHKey(user, sshPublicKey string) (*oslogin.LoginProfile, error) {
901+
func (d *driverGCE) ImportOSLoginSSHKey(user, sshPublicKey string, expirationTimeUsec *int64) (*oslogin.LoginProfile, error) {
902902
parent := fmt.Sprintf("users/%s", user)
903903

904-
resp, err := d.osLoginService.Users.ImportSshPublicKey(parent, &oslogin.SshPublicKey{
904+
sshKey := &oslogin.SshPublicKey{
905905
Key: sshPublicKey,
906-
}).Do()
906+
}
907+
if expirationTimeUsec != nil {
908+
sshKey.ExpirationTimeUsec = *expirationTimeUsec
909+
}
910+
911+
resp, err := d.osLoginService.Users.ImportSshPublicKey(parent, sshKey).Do()
907912
if err != nil {
908913
return nil, err
909914
}

lib/common/driver_mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func (d *DriverMock) CreateOrResetWindowsPassword(instance, zone string, c *Wind
379379
return resultCh, d.CreateOrResetWindowsPasswordErr
380380
}
381381

382-
func (d *DriverMock) ImportOSLoginSSHKey(user, key string) (*oslogin.LoginProfile, error) {
382+
func (d *DriverMock) ImportOSLoginSSHKey(user, key string, expirationTimeUsec *int64) (*oslogin.LoginProfile, error) {
383383
account := oslogin.PosixAccount{Primary: true, Username: "testing_packer_io"}
384384
profile := oslogin.LoginProfile{
385385
PosixAccounts: []*oslogin.PosixAccount{&account},

0 commit comments

Comments
 (0)