github.com/Venafi/vcert/v5@v5.10.2/pkg/playbook/app/domain/authentication_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 "testing" 21 22 "github.com/Venafi/vcert/v5/pkg/endpoint" 23 "github.com/Venafi/vcert/v5/pkg/venafi" 24 "github.com/stretchr/testify/suite" 25 "gopkg.in/yaml.v3" 26 ) 27 28 const examplePlaybook = `certificateTasks: 29 - name: foo 30 config: 31 connection: 32 credentials: 33 accessToken: "123456" 34 apiKey: xyz789 35 clientId: clientID 36 clientSecret: clientSecret 37 externalJWT: tokenJWT 38 idP: 39 audience: some audience 40 tokenURL: some.token.url 41 p12Task: foo 42 refreshToken: abcdef 43 scope: noScope 44 tokenURL: venafi.com/tokenurl 45 insecure: true 46 platform: VAAS 47 trustBundle: some/path.txt 48 url: foo.bar.com 49 ` 50 51 type AuthenticationSuite struct { 52 suite.Suite 53 } 54 55 func (s *AuthenticationSuite) SetupTest() {} 56 57 func TestAuthentication(t *testing.T) { 58 suite.Run(t, new(AuthenticationSuite)) 59 } 60 61 func (s *AuthenticationSuite) TestAuthentication_MarshalIdentityProvider() { 62 p := Playbook{ 63 CertificateTasks: CertificateTasks{ 64 CertificateTask{ 65 Name: "foo", 66 }, 67 }, 68 Config: Config{ 69 Connection: Connection{ 70 Credentials: Authentication{ 71 Authentication: endpoint.Authentication{ 72 AccessToken: "123456", 73 RefreshToken: "abcdef", 74 APIKey: "xyz789", 75 ExternalJWT: "tokenJWT", 76 ClientId: "clientID", 77 ClientSecret: "clientSecret", 78 Scope: "noScope", 79 TokenURL: "venafi.com/tokenurl", 80 IdentityProvider: &endpoint.OAuthProvider{ 81 TokenURL: "some.token.url", 82 Audience: "some audience", 83 }, 84 }, 85 P12Task: "foo", 86 }, 87 Insecure: true, 88 Platform: venafi.TLSPCloud, 89 TrustBundlePath: "some/path.txt", 90 URL: "foo.bar.com", 91 }, 92 }, 93 } 94 95 data, err := yaml.Marshal(p) 96 s.NoError(err) 97 s.NotNil(data) 98 s.Equal([]byte(examplePlaybook), data) 99 } 100 101 func (s *AuthenticationSuite) TestAuthentication_UnmarshalIdentityProvider() { 102 playbook := NewPlaybook() 103 err := yaml.Unmarshal([]byte(examplePlaybook), &playbook) 104 s.NoError(err) 105 s.Equal(1, len(playbook.CertificateTasks)) 106 s.Equal("foo", playbook.CertificateTasks[0].Name) 107 108 s.NotNil(playbook.Config.Connection) 109 s.True(playbook.Config.Connection.Insecure) 110 s.Equal(venafi.TLSPCloud, playbook.Config.Connection.Platform) 111 s.Equal("some/path.txt", playbook.Config.Connection.TrustBundlePath) 112 s.Equal("foo.bar.com", playbook.Config.Connection.URL) 113 114 s.NotNil(playbook.Config.Connection.Credentials) 115 s.Equal("foo", playbook.Config.Connection.Credentials.P12Task) 116 s.Equal("123456", playbook.Config.Connection.Credentials.AccessToken) 117 s.Equal("abcdef", playbook.Config.Connection.Credentials.RefreshToken) 118 s.Equal("xyz789", playbook.Config.Connection.Credentials.APIKey) 119 s.Equal("tokenJWT", playbook.Config.Connection.Credentials.ExternalJWT) 120 s.Equal("venafi.com/tokenurl", playbook.Config.Connection.Credentials.TokenURL) 121 s.Equal("clientID", playbook.Config.Connection.Credentials.ClientId) 122 s.Equal("clientSecret", playbook.Config.Connection.Credentials.ClientSecret) 123 s.Equal("noScope", playbook.Config.Connection.Credentials.Scope) 124 s.NotNil(playbook.Config.Connection.Credentials.IdentityProvider) 125 s.Equal("some.token.url", playbook.Config.Connection.Credentials.IdentityProvider.TokenURL) 126 s.Equal("some audience", playbook.Config.Connection.Credentials.IdentityProvider.Audience) 127 }