github.com/Venafi/vcert/v5@v5.10.2/pkg/playbook/app/domain/playbook_test.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  	"fmt"
    21  	"runtime"
    22  	"testing"
    23  
    24  	"github.com/Venafi/vcert/v5/pkg/endpoint"
    25  	"github.com/Venafi/vcert/v5/pkg/venafi"
    26  	"github.com/stretchr/testify/suite"
    27  )
    28  
    29  type PlaybookSuite struct {
    30  	suite.Suite
    31  	testCases           []testCase
    32  	nonWindowsTestCases []testCase
    33  	windowsTestCases    []testCase
    34  }
    35  
    36  type testCase struct {
    37  	err  error
    38  	name string
    39  	pb   Playbook
    40  }
    41  
    42  func (s *PlaybookSuite) SetupTest() {
    43  
    44  	req := PlaybookRequest{
    45  		Zone: "My\\App",
    46  		Subject: Subject{
    47  			CommonName: "foo.bar.venafi.com",
    48  		},
    49  	}
    50  
    51  	config := Config{
    52  		Connection: Connection{
    53  			Platform: venafi.TLSPCloud,
    54  			Credentials: Authentication{
    55  				Authentication: endpoint.Authentication{
    56  					APIKey: "foobarGibberish123",
    57  				},
    58  			},
    59  		},
    60  	}
    61  
    62  	s.testCases = []testCase{
    63  		{
    64  			err:  ErrNoConfig,
    65  			name: "NoConfig",
    66  			pb: Playbook{
    67  				Config: Config{},
    68  			},
    69  		},
    70  		{
    71  			err:  ErrNoCredentials,
    72  			name: "EmptyCredentials",
    73  			pb: Playbook{
    74  				Config: Config{
    75  					Connection: Connection{
    76  						TrustBundlePath: "asd",
    77  						URL:             "foo",
    78  					},
    79  				},
    80  			},
    81  		},
    82  		{
    83  			err:  ErrMultipleCredentials,
    84  			name: "MultipleCredentials",
    85  			pb: Playbook{
    86  				Config: Config{
    87  					Connection: Connection{
    88  						Credentials: Authentication{
    89  							Authentication: endpoint.Authentication{
    90  								AccessToken: "foobar123",
    91  								APIKey:      "xyz456abc",
    92  							},
    93  						},
    94  					},
    95  				},
    96  			},
    97  		},
    98  		{
    99  			err:  ErrNoTPPURL,
   100  			name: "TPPEmptyURL",
   101  			pb: Playbook{
   102  				Config: Config{
   103  					Connection: Connection{
   104  						Credentials: Authentication{
   105  							Authentication: endpoint.Authentication{
   106  								AccessToken: "someToken",
   107  							},
   108  						},
   109  					},
   110  				},
   111  			},
   112  		},
   113  		{
   114  			err:  ErrTrustBundleNotExist,
   115  			name: "TrustBundleNotExist",
   116  			pb: Playbook{
   117  				Config: Config{
   118  					Connection: Connection{
   119  						Credentials: Authentication{
   120  							Authentication: endpoint.Authentication{
   121  								AccessToken: "someToken",
   122  							},
   123  						},
   124  						URL:             "https://foo.bar.kwan",
   125  						TrustBundlePath: "/foo/bar/bundle.pem",
   126  					},
   127  				},
   128  			},
   129  		},
   130  		{
   131  			err:  ErrNoTasks,
   132  			name: "NoTasks",
   133  			pb: Playbook{
   134  				Config:           config,
   135  				CertificateTasks: nil,
   136  			},
   137  		},
   138  		{
   139  			err:  ErrNoRequestZone,
   140  			name: "NoRequestZone",
   141  			pb: Playbook{
   142  				Config: config,
   143  				CertificateTasks: CertificateTasks{
   144  					{
   145  						Request: PlaybookRequest{},
   146  					},
   147  				},
   148  			},
   149  		},
   150  		{
   151  			err:  ErrNoRequestCN,
   152  			name: "NoRequestCN",
   153  			pb: Playbook{
   154  				Config: config,
   155  				CertificateTasks: CertificateTasks{
   156  					{
   157  						Request: PlaybookRequest{
   158  							Zone: "My\\App",
   159  						},
   160  					},
   161  				},
   162  			},
   163  		},
   164  
   165  		{
   166  			err:  ErrNoInstallations,
   167  			name: "NoInstallationInTask",
   168  			pb: Playbook{
   169  				Config: config,
   170  				CertificateTasks: CertificateTasks{
   171  					{
   172  						Request: req,
   173  					},
   174  				},
   175  			},
   176  		},
   177  		{
   178  			err:  ErrUndefinedInstallationFormat,
   179  			name: "InvalidInstallationType",
   180  			pb: Playbook{
   181  				Config: config,
   182  				CertificateTasks: CertificateTasks{
   183  					{
   184  						Request: req,
   185  						Installations: Installations{
   186  							{
   187  								Type: FormatUnknown,
   188  								File: "something",
   189  							},
   190  						},
   191  					},
   192  				},
   193  			},
   194  		},
   195  
   196  		{
   197  			err:  ErrNoInstallationFile,
   198  			name: "NoJKSLocation",
   199  			pb: Playbook{
   200  				Config: config,
   201  				CertificateTasks: CertificateTasks{
   202  					{
   203  						Request: req,
   204  						Installations: Installations{
   205  							{
   206  								Type: FormatJKS,
   207  							},
   208  						},
   209  					},
   210  				},
   211  			},
   212  		},
   213  		{
   214  			err:  ErrNoJKSAlias,
   215  			name: "NoJKSAlias",
   216  			pb: Playbook{
   217  				Config: config,
   218  				CertificateTasks: CertificateTasks{
   219  					CertificateTask{
   220  						Name:    "testTask",
   221  						Request: req,
   222  						Installations: Installations{
   223  							Installation{
   224  								Type: FormatJKS,
   225  								File: "somewhere",
   226  							},
   227  						},
   228  					},
   229  				},
   230  			},
   231  		},
   232  		{
   233  			err:  ErrNoJKSPassword,
   234  			name: "NoJKSPassword",
   235  			pb: Playbook{
   236  				Config: config,
   237  				CertificateTasks: CertificateTasks{
   238  					CertificateTask{
   239  						Name:    "testTask",
   240  						Request: req,
   241  						Installations: Installations{
   242  							Installation{
   243  								Type:     FormatJKS,
   244  								File:     "somewhere",
   245  								JKSAlias: "someAlias",
   246  							},
   247  						},
   248  					},
   249  				},
   250  			},
   251  		},
   252  		{
   253  			err:  ErrJKSPasswordLength,
   254  			name: "JKSPasswordTooShort",
   255  			pb: Playbook{
   256  				Config: config,
   257  				CertificateTasks: CertificateTasks{
   258  					CertificateTask{
   259  						Name:    "testTask",
   260  						Request: req,
   261  						Installations: Installations{
   262  							Installation{
   263  								Type:        FormatJKS,
   264  								File:        "somewhere",
   265  								JKSAlias:    "alias",
   266  								JKSPassword: "abc12",
   267  							},
   268  						},
   269  					},
   270  				},
   271  			},
   272  		},
   273  
   274  		{
   275  			err:  ErrNoInstallationFile,
   276  			name: "NoPEMLocation",
   277  			pb: Playbook{
   278  				Config: config,
   279  				CertificateTasks: CertificateTasks{
   280  					{
   281  						Request: req,
   282  						Installations: Installations{
   283  							{
   284  								Type: FormatPEM,
   285  							},
   286  						},
   287  					},
   288  				},
   289  			},
   290  		},
   291  		{
   292  			err:  ErrNoChainFile,
   293  			name: "NoPEMChainFile",
   294  			pb: Playbook{
   295  				Config: config,
   296  				CertificateTasks: CertificateTasks{
   297  					CertificateTask{
   298  						Name:    "testTask",
   299  						Request: req,
   300  						Installations: Installations{
   301  							Installation{
   302  								Type: FormatPEM,
   303  								File: "path/to/cert.cer",
   304  							},
   305  						},
   306  					},
   307  				},
   308  			},
   309  		},
   310  		{
   311  			err:  ErrNoKeyFile,
   312  			name: "NoPEMKeyFile",
   313  			pb: Playbook{
   314  				Config: config,
   315  				CertificateTasks: CertificateTasks{
   316  					CertificateTask{
   317  						Name:    "testTask",
   318  						Request: req,
   319  						Installations: Installations{
   320  							Installation{
   321  								Type:      FormatPEM,
   322  								File:      "somewhere",
   323  								ChainFile: "chain.pem",
   324  							},
   325  						},
   326  					},
   327  				},
   328  			},
   329  		},
   330  
   331  		{
   332  			err:  ErrNoInstallationFile,
   333  			name: "NoPKCS12Location",
   334  			pb: Playbook{
   335  				Config: config,
   336  				CertificateTasks: CertificateTasks{
   337  					{
   338  						Request: req,
   339  						Installations: Installations{
   340  							{
   341  								Type: FormatPKCS12,
   342  							},
   343  						},
   344  					},
   345  				},
   346  			},
   347  		},
   348  
   349  		{
   350  			err:  nil,
   351  			name: "ValidPEMConfig",
   352  			pb: Playbook{
   353  				Config: config,
   354  				CertificateTasks: CertificateTasks{
   355  					{
   356  						Name:    "testTask",
   357  						Request: req,
   358  						Installations: Installations{
   359  							{
   360  								Type:      FormatPEM,
   361  								File:      "/foo/bar/pem/cer.cer",
   362  								ChainFile: "/foo/bar/pem/chain.pem",
   363  								KeyFile:   "/foo/bar/pem/key.pem",
   364  							},
   365  						},
   366  					},
   367  				},
   368  			},
   369  		},
   370  		{
   371  			err:  nil,
   372  			name: "ValidJKSConfig",
   373  			pb: Playbook{
   374  				Config: config,
   375  				CertificateTasks: CertificateTasks{
   376  					{
   377  						Name:    "testTask",
   378  						Request: req,
   379  						Installations: Installations{
   380  							{
   381  								Type:        FormatJKS,
   382  								File:        "somewhere",
   383  								JKSAlias:    "alias",
   384  								JKSPassword: "abc123",
   385  							},
   386  						},
   387  					},
   388  				},
   389  			},
   390  		},
   391  		{
   392  			err:  nil,
   393  			name: "ValidPKCS12Config",
   394  			pb: Playbook{
   395  				Config: config,
   396  				CertificateTasks: CertificateTasks{
   397  					{
   398  						Name:    "testTask",
   399  						Request: req,
   400  						Installations: Installations{
   401  							{
   402  								Type:        FormatPKCS12,
   403  								File:        "somewhere",
   404  								P12Password: "foo123",
   405  							},
   406  						},
   407  					},
   408  				},
   409  			},
   410  		},
   411  	}
   412  
   413  	s.nonWindowsTestCases = []testCase{
   414  		{
   415  			err:  ErrNoCAPILocation,
   416  			name: "NoCAPILocation",
   417  			pb: Playbook{
   418  				Config: config,
   419  				CertificateTasks: CertificateTasks{
   420  					{
   421  						Request: req,
   422  						Installations: Installations{
   423  							{
   424  								Type: FormatCAPI,
   425  							},
   426  						},
   427  					},
   428  				},
   429  			},
   430  		},
   431  		{
   432  			err:  ErrCAPIOnNonWindows,
   433  			name: "CAPIOnNonWindows",
   434  			pb: Playbook{
   435  				Config: config,
   436  				CertificateTasks: CertificateTasks{
   437  					CertificateTask{
   438  						Name:    "testTask",
   439  						Request: req,
   440  						Installations: Installations{
   441  							Installation{
   442  								Type:     FormatCAPI,
   443  								Location: "somewhere",
   444  							},
   445  						},
   446  					},
   447  				},
   448  			},
   449  		},
   450  	}
   451  
   452  	s.windowsTestCases = []testCase{
   453  		{
   454  			err:  ErrNoCAPILocation,
   455  			name: "NoCAPILocation",
   456  			pb: Playbook{
   457  				Config: config,
   458  				CertificateTasks: CertificateTasks{
   459  					{
   460  						Request: req,
   461  						Installations: Installations{
   462  							{
   463  								Type: FormatCAPI,
   464  							},
   465  						},
   466  					},
   467  				},
   468  			},
   469  		},
   470  		{
   471  			err:  ErrMalformedCAPILocation,
   472  			name: "MalformedCAPILocation",
   473  			pb: Playbook{
   474  				Config: config,
   475  				CertificateTasks: CertificateTasks{
   476  					CertificateTask{
   477  						Name:    "testTask",
   478  						Request: req,
   479  						Installations: Installations{
   480  							Installation{
   481  								Type:     FormatCAPI,
   482  								Location: "somewhere",
   483  							},
   484  						},
   485  					},
   486  				},
   487  			},
   488  		},
   489  		{
   490  			err:  ErrInvalidCAPILocation,
   491  			name: "InvalidCAPILocation",
   492  			pb: Playbook{
   493  				Config: config,
   494  				CertificateTasks: CertificateTasks{
   495  					CertificateTask{
   496  						Name:    "testTask",
   497  						Request: req,
   498  						Installations: Installations{
   499  							Installation{
   500  								Type:     FormatCAPI,
   501  								Location: "somewhere\\MY",
   502  							},
   503  						},
   504  					},
   505  				},
   506  			},
   507  		},
   508  		{
   509  			err:  ErrInvalidCAPIStoreName,
   510  			name: "InvalidCAPIStoreName",
   511  			pb: Playbook{
   512  				Config: config,
   513  				CertificateTasks: CertificateTasks{
   514  					CertificateTask{
   515  						Name:    "testTask",
   516  						Request: req,
   517  						Installations: Installations{
   518  							Installation{
   519  								Type:     FormatCAPI,
   520  								Location: "LocalMachine\\foo",
   521  							},
   522  						},
   523  					},
   524  				},
   525  			},
   526  		},
   527  		{
   528  			err:  nil,
   529  			name: "ValidCAPIConfig",
   530  			pb: Playbook{
   531  				Config: config,
   532  				CertificateTasks: CertificateTasks{
   533  					CertificateTask{
   534  						Name:    "testTask",
   535  						Request: req,
   536  						Installations: Installations{
   537  							Installation{
   538  								Type:     FormatCAPI,
   539  								Location: "LocalMachine\\MY",
   540  							},
   541  						},
   542  					},
   543  				},
   544  			},
   545  		},
   546  	}
   547  }
   548  
   549  func TestPlaybook(t *testing.T) {
   550  	suite.Run(t, new(PlaybookSuite))
   551  }
   552  
   553  func (s *PlaybookSuite) TestPlaybook_New() {
   554  	pb := NewPlaybook()
   555  
   556  	s.Equal(DefaultFilepath, pb.Location)
   557  	s.Empty(pb.CertificateTasks)
   558  }
   559  
   560  func (s *PlaybookSuite) TestPlaybook_IsValid() {
   561  	testCases := s.testCases
   562  	if runtime.GOOS == "windows" {
   563  		fmt.Print("Windows environment")
   564  		testCases = append(testCases, s.windowsTestCases...)
   565  	} else {
   566  		fmt.Println("NON-Windows environment")
   567  		testCases = append(testCases, s.nonWindowsTestCases...)
   568  	}
   569  
   570  	for _, tc := range testCases {
   571  		s.Run(tc.name, func() {
   572  			_, err := tc.pb.IsValid()
   573  			if tc.err == nil {
   574  				s.Nil(err)
   575  			} else {
   576  				s.Error(err, tc.err.Error())
   577  			}
   578  		})
   579  	}
   580  }