github.com/Venafi/vcert/v5@v5.10.2/pkg/playbook/app/domain/playbook.go (about)

     1  /*
     2   * Copyright 2023 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 domain
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  
    23  	"github.com/Venafi/vcert/v5/pkg/venafi"
    24  )
    25  
    26  const (
    27  	DefaultFilepath = "./playbook.yaml"
    28  	tlspcURL        = "api.venafi.cloud"
    29  )
    30  
    31  // Playbook represents a set of tasks  to run.
    32  //
    33  // The Config object holds the values required to connect to a Venafi platform.
    34  //
    35  // A task includes:
    36  //   - a Request object that defines the values of the certificate to request
    37  //   - a list of locations where the certificate will be installed
    38  type Playbook struct {
    39  	CertificateTasks CertificateTasks `yaml:"certificateTasks,omitempty"`
    40  	Config           Config           `yaml:"config,omitempty"`
    41  	Location         string           `yaml:"-"`
    42  }
    43  
    44  // NewPlaybook returns a Playbook with some default values
    45  func NewPlaybook() Playbook {
    46  	return Playbook{
    47  		CertificateTasks: make(CertificateTasks, 0),
    48  		Config: Config{
    49  			Connection: Connection{
    50  				Platform:        venafi.TLSPCloud,
    51  				URL:             tlspcURL,
    52  				TrustBundlePath: "",
    53  			},
    54  		},
    55  		Location: DefaultFilepath,
    56  	}
    57  }
    58  
    59  // IsValid returns true if the playbook object has the minimum required values to run
    60  func (p Playbook) IsValid() (bool, error) {
    61  	var rErr error = nil
    62  	rValid := true
    63  
    64  	// Check that the specified config is valid
    65  	valid, err := p.Config.IsValid()
    66  	rErr = errors.Join(rErr, err)
    67  	rValid = rValid && valid
    68  
    69  	// There is at least one task to execute
    70  	if len(p.CertificateTasks) < 1 {
    71  		rValid = false
    72  		rErr = errors.Join(rErr, ErrNoTasks)
    73  	}
    74  
    75  	taskNames := make(map[string]bool)
    76  	// Check that the included certificate tasks are valid
    77  	for _, t := range p.CertificateTasks {
    78  		// Check that there are not multiple tasks with the same name
    79  		if !taskNames[t.Name] {
    80  			taskNames[t.Name] = true
    81  		} else {
    82  			rErr = errors.Join(rErr, fmt.Errorf("task '%s' is defined multiple times", t.Name))
    83  			rValid = false
    84  		}
    85  
    86  		_, err := t.IsValid()
    87  		if err != nil {
    88  			rErr = errors.Join(rErr, fmt.Errorf("task '%s' is invalid: %w", t.Name, err))
    89  			rValid = false
    90  		}
    91  	}
    92  
    93  	return rValid, rErr
    94  
    95  }