github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/pkg/valid/validations.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package valid
    18  
    19  import (
    20  	"regexp"
    21  	"strings"
    22  )
    23  
    24  const (
    25  	MaxAppNameLen        = 39
    26  	AppNameRegExp        = `\A[a-z0-9]+(?:-[a-z0-9]+)*\z`
    27  	TeamNameRegExp       = AppNameRegExp
    28  	EnvNameRegExp        = AppNameRegExp
    29  	SHA1CommitIDLength   = 40
    30  	commitIDPrefixRegExp = `^[0-9a-fA-F]*$`
    31  )
    32  
    33  var (
    34  	applicationNameRx = regexp.MustCompile(AppNameRegExp)
    35  	teamNameRx        = regexp.MustCompile(TeamNameRegExp)
    36  	envNameRx         = regexp.MustCompile(EnvNameRegExp)
    37  	commitIDPrefixRx  = regexp.MustCompile(commitIDPrefixRegExp)
    38  )
    39  
    40  // {application}-{environment} should be a valid dns name
    41  func EnvironmentName(env string) bool {
    42  	return len(env) < 21 && envNameRx.MatchString(env)
    43  }
    44  func ApplicationName(name string) bool {
    45  	return len(name) <= MaxAppNameLen && applicationNameRx.MatchString(name)
    46  }
    47  
    48  func TeamName(name string) bool {
    49  	return len(name) < 21 && teamNameRx.MatchString(name)
    50  }
    51  
    52  // Lock names must be valid file names
    53  func LockId(lockId string) bool {
    54  	return len(lockId) < 100 && len(lockId) > 1 && lockId != ".." && lockId != "." && !strings.ContainsAny(lockId, "/")
    55  }
    56  
    57  func SHA1CommitID(commitID string) bool {
    58  	if len(commitID) != SHA1CommitIDLength {
    59  		return false
    60  	}
    61  	return commitIDPrefixRx.MatchString(commitID)
    62  }
    63  
    64  func SHA1CommitIDPrefix(prefix string) bool {
    65  	return commitIDPrefixRx.MatchString(prefix)
    66  }