-
Notifications
You must be signed in to change notification settings - Fork 1
/
bosh_config.go
157 lines (134 loc) · 4.4 KB
/
bosh_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright 2022 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"fmt"
"github.com/pkg/errors"
)
type Bosh struct {
URL string `yaml:"url" mapstructure:"url"`
AllProxy string `yaml:"all_proxy" mapstructure:"all_proxy"`
TrustedCert string `yaml:"root_ca_cert" mapstructure:"root_ca_cert"`
Authentication Authentication
Deployment string
}
type Authentication struct {
Basic UserCredentials
UAA UAAAuthentication
}
func (a Authentication) Validate(URLRequired bool) error {
uaaSet := a.UAA.IsSet()
basicSet := a.Basic.IsSet()
switch {
case !uaaSet && !basicSet:
return fmt.Errorf("must specify an authentication type")
case uaaSet && basicSet:
return fmt.Errorf("cannot specify both basic and UAA authentication")
case uaaSet:
if err := a.UAA.Validate(URLRequired); err != nil {
return NewFieldError("authentication.uaa", err)
}
case basicSet:
if err := a.Basic.Validate(); err != nil {
return NewFieldError("authentication.basic", err)
}
}
return nil
}
func (a Authentication) IsSet() bool {
uaaIsSet := a.UAA.IsSet()
basicSet := a.Basic.IsSet()
return uaaIsSet || basicSet
}
type UAAAuthentication struct {
URL string
ClientCredentials ClientCredentials `yaml:"client_credentials" mapstructure:"client_credentials"`
UserCredentials UserCredentials `yaml:"user_credentials" mapstructure:"user_credentials"`
}
func (a UAAAuthentication) IsSet() bool {
clientCredentialsSet := a.ClientCredentials.IsSet()
userCredentialsSet := a.UserCredentials.IsSet()
return clientCredentialsSet || userCredentialsSet
}
type UserCredentials struct {
Username string
Password string
}
func (c UserCredentials) IsSet() bool {
return c != UserCredentials{}
}
func (c UserCredentials) Validate() error {
if c.Username == "" {
return NewFieldError("username", errors.New("can't be empty"))
}
if c.Password == "" {
return NewFieldError("password", errors.New("can't be empty"))
}
return nil
}
type ClientCredentials struct {
ID string `yaml:"client_id" mapstructure:"client_id"`
Secret string `yaml:"client_secret" mapstructure:"client_secret"`
}
func (cc ClientCredentials) IsSet() bool {
return cc != ClientCredentials{}
}
func (cc ClientCredentials) Validate() error {
if cc.ID == "" {
return NewFieldError("client_id", errors.New("can't be empty"))
}
if cc.Secret == "" {
return NewFieldError("client_secret", errors.New("can't be empty"))
}
return nil
}
func (b Bosh) Validate() error {
if b.URL == "" {
return fmt.Errorf("must specify bosh url")
}
if b.AllProxy == "" {
return fmt.Errorf("must specify bosh all_proxy")
}
return b.Authentication.Validate(false)
}
func (b Bosh) IsSet() bool {
return b != Bosh{}
}
func (a UAAAuthentication) Validate(URLRequired bool) error {
return validateAuthenticationFields(URLRequired, a.URL, a.ClientCredentials, a.UserCredentials)
}
func validateAuthenticationFields(URLRequired bool, url string, clientCredentials ClientCredentials, userCredentials UserCredentials) error {
urlIsSet := url != ""
clientCredentialsSet := clientCredentials.IsSet()
userCredentialsSet := userCredentials.IsSet()
switch {
case !urlIsSet && !clientCredentialsSet && !userCredentialsSet:
return fmt.Errorf("must specify UAA authentication")
case !urlIsSet && URLRequired:
return NewFieldError("uaa url", errors.New("can't be empty"))
case !clientCredentialsSet && !userCredentialsSet:
return fmt.Errorf("authentication should contain either user_credentials or client_credentials")
case clientCredentialsSet && userCredentialsSet:
return fmt.Errorf("contains both client and user credentials")
case clientCredentialsSet:
if err := clientCredentials.Validate(); err != nil {
return NewFieldError("client_credentials", err)
}
case userCredentialsSet:
if err := userCredentials.Validate(); err != nil {
return NewFieldError("user_credentials", err)
}
}
return nil
}