github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/util/validation/stack.go (about)

     1  // Copyright 2016-2019, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package validation
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"regexp"
    21  
    22  	"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
    23  )
    24  
    25  // validateStackName checks if s is a valid stack name, otherwise returns a descriptive error.
    26  // This should match the stack naming rules enforced by the Pulumi Service.
    27  func validateStackName(s string) error {
    28  	stackNameRE := regexp.MustCompile("^[a-zA-Z0-9-_.]{1,100}$")
    29  	if stackNameRE.MatchString(s) {
    30  		return nil
    31  	}
    32  	return errors.New("a stack name may only contain alphanumeric, hyphens, underscores, or periods")
    33  }
    34  
    35  // validateStackTagName checks if s is a valid stack tag name, otherwise returns a descriptive error.
    36  // This should match the stack naming rules enforced by the Pulumi Service.
    37  func validateStackTagName(s string) error {
    38  	const maxTagName = 40
    39  
    40  	if len(s) == 0 {
    41  		return fmt.Errorf("invalid stack tag %q", s)
    42  	}
    43  	if len(s) > maxTagName {
    44  		return fmt.Errorf("stack tag %q is too long (max length %d characters)", s, maxTagName)
    45  	}
    46  
    47  	var tagNameRE = regexp.MustCompile("^[a-zA-Z0-9-_.:]{1,40}$")
    48  	if tagNameRE.MatchString(s) {
    49  		return nil
    50  	}
    51  	return errors.New("stack tag names may only contain alphanumerics, hyphens, underscores, periods, or colons")
    52  }
    53  
    54  // ValidateStackTags validates the tag names and values.
    55  func ValidateStackTags(tags map[apitype.StackTagName]string) error {
    56  	const maxTagValue = 256
    57  
    58  	for t, v := range tags {
    59  		if err := validateStackTagName(t); err != nil {
    60  			return err
    61  		}
    62  		if len(v) > maxTagValue {
    63  			return fmt.Errorf("stack tag %q value is too long (max length %d characters)", t, maxTagValue)
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  // ValidateStackProperties validates the stack name and its tags to confirm they adhear to various
    71  // naming and length restrictions.
    72  func ValidateStackProperties(stack string, tags map[apitype.StackTagName]string) error {
    73  	const maxStackName = 100 // Derived from the regex in validateStackName.
    74  	if len(stack) > maxStackName {
    75  		return fmt.Errorf("stack name too long (max length %d characters)", maxStackName)
    76  	}
    77  	if err := validateStackName(stack); err != nil {
    78  		return err
    79  	}
    80  
    81  	// Ensure tag values won't be rejected by the Pulumi Service. We do not validate that their
    82  	// values make sense, e.g. ProjectRuntimeTag is a supported runtime.
    83  	return ValidateStackTags(tags)
    84  }