2016-04-21 19:50:03 -04:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-04-21 19:50:03 -04:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2016-04-21 19:50:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepGetCertificate struct {
|
2017-03-28 21:02:51 -04:00
|
|
|
client *AzureClient
|
|
|
|
get func(keyVaultName string, secretName string) (string, error)
|
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
|
|
|
pause func()
|
2016-04-21 19:50:03 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func NewStepGetCertificate(client *AzureClient, ui packersdk.Ui) *StepGetCertificate {
|
2016-04-21 19:50:03 -04:00
|
|
|
var step = &StepGetCertificate{
|
|
|
|
client: client,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
pause: func() { time.Sleep(30 * time.Second) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.get = step.getCertificateUrl
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepGetCertificate) getCertificateUrl(keyVaultName string, secretName string) (string, error) {
|
|
|
|
secret, err := s.client.GetSecret(keyVaultName, secretName)
|
|
|
|
if err != nil {
|
2017-06-08 20:57:59 -04:00
|
|
|
s.say(s.client.LastError.Error())
|
2016-04-21 19:50:03 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *secret.ID, err
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepGetCertificate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-04-21 19:50:03 -04:00
|
|
|
s.say("Getting the certificate's URL ...")
|
|
|
|
|
|
|
|
var keyVaultName = state.Get(constants.ArmKeyVaultName).(string)
|
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" -> Key Vault Name : '%s'", keyVaultName))
|
|
|
|
s.say(fmt.Sprintf(" -> Key Vault Secret Name : '%s'", DefaultSecretName))
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var url string
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
url, err = s.get(keyVaultName, DefaultSecretName)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" ...failed to get certificate URL, retry(%d)", i))
|
|
|
|
s.pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
state.Put(constants.Error, err)
|
|
|
|
s.error(err)
|
|
|
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" -> Certificate URL : '%s'", url))
|
|
|
|
state.Put(constants.ArmCertificateUrl, url)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepGetCertificate) Cleanup(multistep.StateBag) {
|
|
|
|
}
|