github.com/Venafi/vcert/v5@v5.10.2/test/fixtures.go (about)

     1  /*
     2   * Copyright 2018 Venafi, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *  http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package test
    18  
    19  import (
    20  	"crypto/rand"
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/Venafi/vcert/v5/pkg/certificate"
    27  	"github.com/Venafi/vcert/v5/pkg/policy"
    28  )
    29  
    30  func randRunes(n int) string {
    31  	letterRunes := "abcdefghijklmnopqrstuvwxyz"
    32  
    33  	b := make([]byte, n)
    34  	_, _ = rand.Read(b)
    35  
    36  	for i, v := range b {
    37  		b[i] = letterRunes[v%byte(len(letterRunes))]
    38  	}
    39  	return string(b)
    40  }
    41  
    42  func RandCN() string {
    43  	return fmt.Sprintf("t%d-%s.venafi.example.com", time.Now().Unix(), randRunes(4))
    44  }
    45  
    46  func RandSpecificCN(cn string) string {
    47  	return fmt.Sprintf("t%d-%s.%s", time.Now().Unix(), randRunes(4), cn)
    48  }
    49  
    50  func RandAppName() string {
    51  	return fmt.Sprintf("vcert-go-%d-%sAppOpenSource", time.Now().Unix(), randRunes(4))
    52  }
    53  
    54  func RandCitName() string {
    55  	return fmt.Sprintf("t%d-%sCitOpenSource", time.Now().Unix(), randRunes(4))
    56  }
    57  
    58  func RandTppPolicyName() string {
    59  	return fmt.Sprintf("vcert-go-%d-%sPolicyOpenSource", time.Now().Unix(), randRunes(4))
    60  }
    61  
    62  func GetCloudPolicySpecification() *policy.PolicySpecification {
    63  	caName := os.Getenv("CLOUD_CA_NAME")
    64  	validityHours := 120
    65  	wildcardAllowed := true
    66  	serviceGenerated := true
    67  	reuseAllowed := false
    68  	subjectAltNamesAllowed := true
    69  	upnAllowed := false
    70  
    71  	domain := "venafi.com"
    72  	org := "Venafi"
    73  	locality := "Salt Lake City"
    74  	state := "Utah"
    75  	country := "US"
    76  
    77  	defaultKeyType := "RSA"
    78  	defaultKeySize := 2048
    79  
    80  	specification := policy.PolicySpecification{
    81  		Policy: &policy.Policy{
    82  			CertificateAuthority: &caName,
    83  			Domains:              []string{"venafi.com"},
    84  			WildcardAllowed:      &wildcardAllowed,
    85  			MaxValidDays:         &validityHours,
    86  			Subject: &policy.Subject{
    87  				Orgs:       []string{"Venafi"},
    88  				OrgUnits:   []string{"DevOps"},
    89  				Localities: []string{"Salt Lake City"},
    90  				States:     []string{"Utah"},
    91  				Countries:  []string{"US"},
    92  			},
    93  			KeyPair: &policy.KeyPair{
    94  				KeyTypes:         []string{"RSA"},
    95  				RsaKeySizes:      []int{2048},
    96  				ServiceGenerated: &serviceGenerated,
    97  				ReuseAllowed:     &reuseAllowed,
    98  				EllipticCurves:   []string{"P384"},
    99  			},
   100  			SubjectAltNames: &policy.SubjectAltNames{
   101  				DnsAllowed:   &subjectAltNamesAllowed,
   102  				IpAllowed:    &subjectAltNamesAllowed,
   103  				EmailAllowed: &subjectAltNamesAllowed,
   104  				UriAllowed:   &subjectAltNamesAllowed,
   105  				UpnAllowed:   &upnAllowed,
   106  				UriProtocols: []string{"https", "ldaps", "spiffe"},
   107  			},
   108  		},
   109  		Default: &policy.Default{
   110  			Domain: &domain,
   111  			Subject: &policy.DefaultSubject{
   112  				Org:      &org,
   113  				OrgUnits: []string{"DevOps"},
   114  				Locality: &locality,
   115  				State:    &state,
   116  				Country:  &country,
   117  			},
   118  			KeyPair: &policy.DefaultKeyPair{
   119  				KeyType:          &defaultKeyType,
   120  				RsaKeySize:       &defaultKeySize,
   121  				EllipticCurve:    nil,
   122  				ServiceGenerated: nil,
   123  			},
   124  		},
   125  	}
   126  	return &specification
   127  }
   128  
   129  func GetVAASpolicySpecificationEC() *policy.PolicySpecification {
   130  	caName := os.Getenv("CLOUD_CA_NAME")
   131  	maxValidityDays := 90
   132  	wildcardAllowed := false
   133  	serviceGenerated := false
   134  	reuseAllowed := false
   135  	subjectAltNamesAllowed := true
   136  	upnAllowed := true
   137  
   138  	domain := ""
   139  	org := "Venafi Inc."
   140  	locality := "Salt Lake"
   141  	state := "Utah"
   142  	country := "US"
   143  
   144  	defaultKeyType := "ECDSA"
   145  	defaultKeyCurve := "P256"
   146  	keyTypeEC := certificate.KeyTypeECDSA
   147  	keyTypeECstring := keyTypeEC.String()
   148  
   149  	specification := policy.PolicySpecification{
   150  		Policy: &policy.Policy{
   151  			CertificateAuthority: &caName,
   152  			Domains:              []string{"vfidev.com"},
   153  			WildcardAllowed:      &wildcardAllowed,
   154  			MaxValidDays:         &maxValidityDays,
   155  			Subject: &policy.Subject{
   156  				Orgs:       []string{"Venafi Inc."},
   157  				OrgUnits:   []string{"Integrations", "Integration"},
   158  				Localities: []string{"Salt Lake"},
   159  				States:     []string{"Utah"},
   160  				Countries:  []string{"US"},
   161  			},
   162  			KeyPair: &policy.KeyPair{
   163  				KeyTypes:         []string{keyTypeECstring},
   164  				ServiceGenerated: &serviceGenerated,
   165  				ReuseAllowed:     &reuseAllowed,
   166  				EllipticCurves:   []string{"P256", "P384", "P521", "ED25519"},
   167  			},
   168  			SubjectAltNames: &policy.SubjectAltNames{
   169  				DnsAllowed:   &subjectAltNamesAllowed,
   170  				IpAllowed:    &subjectAltNamesAllowed,
   171  				EmailAllowed: &subjectAltNamesAllowed,
   172  				UriAllowed:   &subjectAltNamesAllowed,
   173  				UpnAllowed:   &upnAllowed,
   174  				UriProtocols: []string{"https", "ldaps", "spiffe"},
   175  			},
   176  		},
   177  		Default: &policy.Default{
   178  			Domain: &domain,
   179  			Subject: &policy.DefaultSubject{
   180  				Org:      &org,
   181  				OrgUnits: []string{"Integrations"},
   182  				Locality: &locality,
   183  				State:    &state,
   184  				Country:  &country,
   185  			},
   186  			KeyPair: &policy.DefaultKeyPair{
   187  				KeyType:          &defaultKeyType,
   188  				RsaKeySize:       nil,
   189  				EllipticCurve:    &defaultKeyCurve,
   190  				ServiceGenerated: nil,
   191  			},
   192  		},
   193  	}
   194  	return &specification
   195  }
   196  
   197  func GetTppPolicySpecification() *policy.PolicySpecification {
   198  
   199  	caName := os.Getenv("TPP_CA_NAME")
   200  	validityHours := 120
   201  	wildcardAllowed := true
   202  	serviceGenerated := true
   203  	reuseAllowed := false
   204  	subjectAltNamesAllowedTrue := true
   205  	subjectAltNamesAllowedFalse := false
   206  	autoInstalled := true
   207  
   208  	domain := "venafi.com"
   209  	org := "Venafi"
   210  	locality := "Salt Lake City"
   211  	state := "Utah"
   212  	country := "US"
   213  
   214  	defaultKeyType := "RSA"
   215  	defaultKeySize := 3072
   216  
   217  	specification := policy.PolicySpecification{
   218  		Policy: &policy.Policy{
   219  			CertificateAuthority: &caName,
   220  			Domains:              []string{"venafi.com"},
   221  			WildcardAllowed:      &wildcardAllowed,
   222  			MaxValidDays:         &validityHours,
   223  			AutoInstalled:        &autoInstalled,
   224  			Subject: &policy.Subject{
   225  				Orgs:       []string{"Venafi"},
   226  				OrgUnits:   []string{"DevOps"},
   227  				Localities: []string{"Salt Lake City"},
   228  				States:     []string{"Utah"},
   229  				Countries:  []string{"US"},
   230  			},
   231  			KeyPair: &policy.KeyPair{
   232  				KeyTypes:         []string{"RSA"},
   233  				RsaKeySizes:      []int{3072},
   234  				ServiceGenerated: &serviceGenerated,
   235  				ReuseAllowed:     &reuseAllowed,
   236  				EllipticCurves:   []string{"P384"},
   237  			},
   238  			SubjectAltNames: &policy.SubjectAltNames{
   239  				DnsAllowed:   &subjectAltNamesAllowedTrue,
   240  				IpAllowed:    &subjectAltNamesAllowedTrue,
   241  				EmailAllowed: &subjectAltNamesAllowedFalse,
   242  				UriAllowed:   &subjectAltNamesAllowedFalse,
   243  				UpnAllowed:   &subjectAltNamesAllowedFalse,
   244  			},
   245  		},
   246  		Default: &policy.Default{
   247  			Domain:        &domain,
   248  			AutoInstalled: &autoInstalled,
   249  			Subject: &policy.DefaultSubject{
   250  				Org:      &org,
   251  				OrgUnits: []string{"DevOps"},
   252  				Locality: &locality,
   253  				State:    &state,
   254  				Country:  &country,
   255  			},
   256  			KeyPair: &policy.DefaultKeyPair{
   257  				KeyType:          &defaultKeyType,
   258  				RsaKeySize:       &defaultKeySize,
   259  				EllipticCurve:    nil,
   260  				ServiceGenerated: nil,
   261  			},
   262  		},
   263  	}
   264  	return &specification
   265  }
   266  
   267  func IsArrayStringEqual(expectedValues, values []string) bool {
   268  
   269  	if len(expectedValues) != len(values) {
   270  		return false
   271  	}
   272  
   273  	for i, currentValue := range expectedValues {
   274  		values[i] = UnifyECvalue(values[i])
   275  		if currentValue != values[i] {
   276  			return false
   277  		}
   278  	}
   279  
   280  	return true
   281  }
   282  
   283  func UnifyECvalue(value string) string {
   284  	switch v := strings.ToLower(value); v {
   285  	case "ecdsa", "ec", "ecc":
   286  		EC := certificate.KeyTypeECDSA
   287  		value = EC.String()
   288  	}
   289  	return value
   290  }
   291  
   292  func StringArraysContainsSameValues(s1 []string, s2 []string) bool {
   293  	if len(s1) != len(s2) {
   294  		return false
   295  	}
   296  
   297  	for _, value1 := range s1 {
   298  		found := false
   299  		for _, value2 := range s2 {
   300  			if value1 == value2 {
   301  				found = true
   302  				break
   303  			}
   304  		}
   305  		if !found {
   306  			return false
   307  		}
   308  	}
   309  
   310  	return true
   311  }
   312  
   313  func IsArrayIntEqual(expectedValues, values []int) bool {
   314  
   315  	if len(expectedValues) != len(values) {
   316  		return false
   317  	}
   318  
   319  	for i, currentValue := range expectedValues {
   320  
   321  		if currentValue != values[i] {
   322  
   323  			return false
   324  
   325  		}
   326  
   327  	}
   328  
   329  	return true
   330  }
   331  
   332  func RandSshKeyId() string {
   333  	return fmt.Sprintf("vcert-go-%d-%sSSHCert", time.Now().Unix(), randRunes(4))
   334  }