// randomAdminPassword returns a random administrator password for // Windows machines. func randomAdminPassword() string { // We want at least one each of lower-alpha, upper-alpha, and digit. // Allocate 16 of each (randomly), and then the remaining characters // will be randomly chosen from the full set. validRunes := append(utils.LowerAlpha, utils.Digits...) validRunes = append(validRunes, utils.UpperAlpha...) lowerAlpha := utils.RandomString(16, utils.LowerAlpha) upperAlpha := utils.RandomString(16, utils.UpperAlpha) digits := utils.RandomString(16, utils.Digits) mixed := utils.RandomString(16, validRunes) password := []rune(lowerAlpha + upperAlpha + digits + mixed) for i := len(password) - 1; i >= 1; i-- { j := rand.Intn(i + 1) password[i], password[j] = password[j], password[i] } return string(password) }
// getMetadata builds the raw "user-defined" metadata for the new // instance (relative to the provided args) and returns it. func getMetadata(args environs.StartInstanceParams, os jujuos.OSType) (map[string]string, error) { userData, err := providerinit.ComposeUserData(args.InstanceConfig, nil, GCERenderer{}) if err != nil { return nil, errors.Annotate(err, "cannot make user data") } logger.Debugf("GCE user data; %d bytes", len(userData)) metadata := make(map[string]string) if isController(args.InstanceConfig) { metadata[metadataKeyIsState] = metadataValueTrue } else { metadata[metadataKeyIsState] = metadataValueFalse } switch os { case jujuos.Ubuntu: // We store a gz snapshop of information that is used by // cloud-init and unpacked in to the /var/lib/cloud/instances folder // for the instance. Due to a limitation with GCE and binary blobs // we base64 encode the data before storing it. metadata[metadataKeyCloudInit] = string(userData) // Valid encoding values are determined by the cloudinit GCE data source. // See: http://cloudinit.readthedocs.org metadata[metadataKeyEncoding] = "base64" authKeys, err := google.FormatAuthorizedKeys(args.InstanceConfig.AuthorizedKeys, "ubuntu") if err != nil { return nil, errors.Trace(err) } metadata[metadataKeySSHKeys] = authKeys case jujuos.Windows: metadata[metadataKeyWindowsUserdata] = string(userData) validChars := append(utils.UpperAlpha, append(utils.LowerAlpha, utils.Digits...)...) // The hostname must have maximum 15 characters winHostname := "juju" + utils.RandomString(11, validChars) metadata[metadataKeyWindowsSysprep] = fmt.Sprintf(winSetHostnameScript, winHostname) default: return nil, errors.Errorf("cannot pack metadata for os %s on the gce provider", os.String()) } return metadata, nil }
// RandomStorageAccountName returns a random storage account name. func RandomStorageAccountName() string { const maxStorageAccountNameLen = 24 validRunes := append(utils.LowerAlpha, utils.Digits...) return utils.RandomString(maxStorageAccountNameLen, validRunes) }
func (randomStringSuite) TestContentInValidRunes(c *gc.C) { s := utils.RandomString(length, validChars) for _, char := range s { c.Assert(string(validChars), jc.Contains, string(char)) } }
func (randomStringSuite) TestLength(c *gc.C) { s := utils.RandomString(length, validChars) c.Assert(s, gc.HasLen, length) }