github.com/hashicorp/vault/sdk@v0.13.0/database/helper/credsutil/credsutil.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package credsutil
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/hashicorp/go-secure-stdlib/base62"
    12  	"github.com/hashicorp/vault/sdk/database/dbplugin"
    13  )
    14  
    15  // CredentialsProducer can be used as an embedded interface in the Database
    16  // definition. It implements the methods for generating user information for a
    17  // particular database type and is used in all the builtin database types.
    18  type CredentialsProducer interface {
    19  	GenerateCredentials(context.Context) (string, error)
    20  	GenerateUsername(dbplugin.UsernameConfig) (string, error)
    21  	GeneratePassword() (string, error)
    22  	GenerateExpiration(time.Time) (string, error)
    23  }
    24  
    25  const (
    26  	reqStr    = `A1a-`
    27  	minStrLen = 10
    28  )
    29  
    30  // RandomAlphaNumeric returns a random string of characters [A-Za-z0-9-]
    31  // of the provided length. The string generated takes up to 4 characters
    32  // of space that are predefined and prepended to ensure password
    33  // character requirements. It also requires a min length of 10 characters.
    34  func RandomAlphaNumeric(length int, prependA1a bool) (string, error) {
    35  	if length < minStrLen {
    36  		return "", fmt.Errorf("minimum length of %d is required", minStrLen)
    37  	}
    38  
    39  	var prefix string
    40  	if prependA1a {
    41  		prefix = reqStr
    42  	}
    43  
    44  	randomStr, err := base62.Random(length - len(prefix))
    45  	if err != nil {
    46  		return "", err
    47  	}
    48  
    49  	return prefix + randomStr, nil
    50  }