Merge pull request #6480 from hashicorp/pr-azure-password-requirements
azure: satisfy Azure password requirements
This commit is contained in:
commit
9c6b4287e5
|
@ -2,13 +2,19 @@ package arm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/packer/builder/azure/common"
|
"github.com/hashicorp/packer/builder/azure/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TempNameAlphabet = "0123456789bcdfghjklmnpqrstvwxyz"
|
TempNameAlphabet = "0123456789bcdfghjklmnpqrstvwxyz"
|
||||||
TempPasswordAlphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
|
numbers = "0123456789"
|
||||||
|
lowerCase = "abcdefghijklmnopqrstuvwxyz"
|
||||||
|
upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
|
TempPasswordAlphabet = numbers + lowerCase + upperCase
|
||||||
)
|
)
|
||||||
|
|
||||||
type TempName struct {
|
type TempName struct {
|
||||||
|
@ -39,8 +45,37 @@ func NewTempName() *TempName {
|
||||||
tempName.VirtualNetworkName = fmt.Sprintf("pkrvn%s", suffix)
|
tempName.VirtualNetworkName = fmt.Sprintf("pkrvn%s", suffix)
|
||||||
tempName.ResourceGroupName = fmt.Sprintf("packer-Resource-Group-%s", suffix)
|
tempName.ResourceGroupName = fmt.Sprintf("packer-Resource-Group-%s", suffix)
|
||||||
|
|
||||||
tempName.AdminPassword = common.RandomString(TempPasswordAlphabet, 32)
|
tempName.AdminPassword = generatePassword()
|
||||||
tempName.CertificatePassword = common.RandomString(TempPasswordAlphabet, 32)
|
tempName.CertificatePassword = common.RandomString(TempPasswordAlphabet, 32)
|
||||||
|
|
||||||
return tempName
|
return tempName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generate a password that is acceptable to Azure
|
||||||
|
// Three of the four items must be met.
|
||||||
|
// 1. Contains an uppercase character
|
||||||
|
// 2. Contains a lowercase character
|
||||||
|
// 3. Contains a numeric digit
|
||||||
|
// 4. Contains a special character
|
||||||
|
func generatePassword() string {
|
||||||
|
var s string
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
s := common.RandomString(TempPasswordAlphabet, 32)
|
||||||
|
if !strings.ContainsAny(s, numbers) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.ContainsAny(s, lowerCase) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.ContainsAny(s, upperCase) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// if an acceptable password cannot be generated in 100 tries, give up
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
|
@ -41,6 +41,20 @@ func TestTempNameShouldCreatePrefixedRandomNames(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTempAdminPassword(t *testing.T) {
|
||||||
|
tempName := NewTempName()
|
||||||
|
|
||||||
|
if !strings.ContainsAny(tempName.AdminPassword, numbers) {
|
||||||
|
t.Errorf("Expected AdminPassword to contain at least one of '%s'!", numbers)
|
||||||
|
}
|
||||||
|
if !strings.ContainsAny(tempName.AdminPassword, lowerCase) {
|
||||||
|
t.Errorf("Expected AdminPassword to contain at least one of '%s'!", lowerCase)
|
||||||
|
}
|
||||||
|
if !strings.ContainsAny(tempName.AdminPassword, upperCase) {
|
||||||
|
t.Errorf("Expected AdminPassword to contain at least one of '%s'!", upperCase)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTempNameShouldHaveSameSuffix(t *testing.T) {
|
func TestTempNameShouldHaveSameSuffix(t *testing.T) {
|
||||||
tempName := NewTempName()
|
tempName := NewTempName()
|
||||||
suffix := tempName.ComputeName[5:]
|
suffix := tempName.ComputeName[5:]
|
||||||
|
|
Loading…
Reference in New Issue