github.com/Venafi/vcert/v5@v5.10.2/pkg/certificate/csrOrigin.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 certificate
    18  
    19  import (
    20  	"strings"
    21  
    22  	"gopkg.in/yaml.v3"
    23  )
    24  
    25  type CSrOriginOption int
    26  
    27  const (
    28  	// LocalGeneratedCSR - this vcert library generates CSR internally based on Request data
    29  	LocalGeneratedCSR CSrOriginOption = iota // local generation is default.
    30  	// ServiceGeneratedCSR - server generate CSR internally based on zone configuration and data from Request
    31  	ServiceGeneratedCSR
    32  	// UserProvidedCSR - client provides CSR from external resource and vcert library just check and send this CSR to server
    33  	UserProvidedCSR
    34  	UnknownCSR
    35  
    36  	// StrLocalGeneratedCSR is the string representations of the LocalGeneratedCSR constant
    37  	StrLocalGeneratedCSR = "local"
    38  	// StrServiceGeneratedCSR is the string representations of the ServiceGeneratedCSR constant
    39  	StrServiceGeneratedCSR = "service"
    40  	// StrUserProvidedCSR is the string representations of the UserProvidedCSR constant
    41  	StrUserProvidedCSR = "file"
    42  	strUnknownCSR      = "unknown"
    43  )
    44  
    45  // String returns a string representation of this object
    46  func (csr *CSrOriginOption) String() string {
    47  	switch *csr {
    48  	case LocalGeneratedCSR:
    49  		return StrLocalGeneratedCSR
    50  	case ServiceGeneratedCSR:
    51  		return StrServiceGeneratedCSR
    52  	case UserProvidedCSR:
    53  		return StrUserProvidedCSR
    54  	default:
    55  		return strUnknownCSR
    56  	}
    57  }
    58  
    59  // ParseCSROrigin returns a CSrOriginOption from a valid string representation
    60  func ParseCSROrigin(value string) CSrOriginOption {
    61  	switch strings.ToLower(value) {
    62  	case StrLocalGeneratedCSR:
    63  		return LocalGeneratedCSR
    64  	case StrServiceGeneratedCSR:
    65  		return ServiceGeneratedCSR
    66  	case StrUserProvidedCSR:
    67  		return UserProvidedCSR
    68  	default:
    69  		return UnknownCSR
    70  	}
    71  }
    72  
    73  // MarshalYAML customizes the behavior of ChainOption when being marshaled into a YAML document.
    74  // The returned value is marshaled in place of the original value implementing Marshaller
    75  func (csr CSrOriginOption) MarshalYAML() (interface{}, error) {
    76  	return csr.String(), nil
    77  }
    78  
    79  // UnmarshalYAML customizes the behavior when being unmarshalled from a YAML document
    80  func (csr *CSrOriginOption) UnmarshalYAML(value *yaml.Node) error {
    81  	var strValue string
    82  	err := value.Decode(&strValue)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	*csr = ParseCSROrigin(strValue)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	return nil
    91  }