-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfiguration.go
195 lines (166 loc) · 6 KB
/
configuration.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
IdentityNow V3 API
Use these APIs to interact with the IdentityNow platform to achieve repeatable, automated processes with greater scalability. We encourage you to join the SailPoint Developer Community forum at https://developer.sailpoint.com/discuss to connect with other developers using our APIs.
API version: 3.0.0
*/
package sailpoint
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/spf13/viper"
)
type PatConfig struct {
ClientID string `mapstructure:"clientid"`
ClientSecret string `mapstructure:"clientsecret"`
AccessToken string `mapstructure:"accesstoken"`
Expiry time.Time `mapstructure:"expiry"`
}
type Token struct {
AccessToken string `mapstructure:"accesstoken"`
Expiry time.Time `mapstructure:"expiry"`
}
type Environment struct {
TenantURL string `mapstructure:"tenanturl"`
BaseURL string `mapstructure:"baseurl"`
Pat PatConfig `mapstructure:"pat"`
OAuth Token `mapstructure:"oauth"`
}
type OrgConfig struct {
//Standard Variables
Debug bool `mapstructure:"debug"`
AuthType string `mapstructure:"authtype"`
ActiveEnvironment string `mapstructure:"activeenvironment"`
Environments map[string]Environment `mapstructure:"environments"`
}
type ClientConfiguration struct {
ClientId string
ClientSecret string
BaseURL string
TokenURL string
Token string
}
// ServerVariable stores the information about a server variable
type ServerVariable struct {
Description string
DefaultValue string
EnumValues []string
}
// Configuration stores the configuration of the API client
type Configuration struct {
Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
Debug bool `json:"debug,omitempty"`
Experimental bool `json:"experimental,omitempty"`
HTTPClient *retryablehttp.Client
ClientConfiguration ClientConfiguration
}
// NewConfiguration returns a new Configuration object
func NewConfiguration(clientConfiguration ClientConfiguration) *Configuration {
cfg := &Configuration{
DefaultHeader: make(map[string]string),
UserAgent: "OpenAPI-Generator/2.1.18/go",
Debug: false,
ClientConfiguration: clientConfiguration,
}
return cfg
}
// NewCLIConfiguration returns a new Configuration object
func NewCLIConfiguration(clientConfiguration ClientConfiguration) *Configuration {
cfg := &Configuration{
DefaultHeader: make(map[string]string),
UserAgent: "SailPoint-CLI/2.1.18/go",
Debug: false,
ClientConfiguration: clientConfiguration,
}
return cfg
}
func localConfig() ClientConfiguration {
executableDir, err := os.Executable()
if err != nil {
panic(fmt.Errorf("unable to find executable directory: %s \n", err))
}
viper.AddConfigPath(filepath.Dir(executableDir))
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetConfigType("json")
if err2 := viper.ReadInConfig(); err != nil {
if _, ok := err2.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
// IGNORE they may be using env vars
} else {
// Config file was found but another error was produced
panic(fmt.Errorf("unable to read config: %s \n", err2))
}
}
var simpleConfig ClientConfiguration
err3 := viper.Unmarshal(&simpleConfig)
if err3 != nil {
panic(fmt.Errorf("unable to decode Config: %s \n", err3))
}
simpleConfig.TokenURL = simpleConfig.BaseURL + "/oauth/token"
return simpleConfig
}
func homeConfig() ClientConfiguration {
home, err := os.UserHomeDir()
if err != nil {
panic(fmt.Errorf("unable to find home directory: %s \n", err))
}
viper.AddConfigPath(filepath.Join(home, ".sailpoint"))
viper.SetConfigName("config")
viper.SetConfigType("yaml")
if err2 := viper.ReadInConfig(); err != nil {
if _, ok := err2.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
// IGNORE they may be using env vars
} else {
// Config file was found but another error was produced
panic(fmt.Errorf("unable to read config: %s \n", err2))
}
}
var config OrgConfig
err3 := viper.Unmarshal(&config)
if err3 != nil {
panic(fmt.Errorf("unable to decode Config: %s \n", err3))
}
var simpleConfig ClientConfiguration
simpleConfig.BaseURL = config.Environments[config.ActiveEnvironment].BaseURL
simpleConfig.ClientId = config.Environments[config.ActiveEnvironment].Pat.ClientID
simpleConfig.ClientSecret = config.Environments[config.ActiveEnvironment].Pat.ClientSecret
simpleConfig.TokenURL = simpleConfig.BaseURL + "/oauth/token"
return simpleConfig
}
func envConfig() ClientConfiguration {
var simpleConfig ClientConfiguration
if os.Getenv("SAIL_BASE_URL") != "" {
simpleConfig.BaseURL = os.Getenv("SAIL_BASE_URL")
}
if os.Getenv("SAIL_CLIENT_ID") != "" {
simpleConfig.ClientId = os.Getenv("SAIL_CLIENT_ID")
}
if os.Getenv("SAIL_CLIENT_SECRET") != "" {
simpleConfig.ClientSecret = os.Getenv("SAIL_CLIENT_SECRET")
}
simpleConfig.TokenURL = simpleConfig.BaseURL + "/oauth/token"
return simpleConfig
}
func NewDefaultConfiguration() *Configuration {
envConfiguration := envConfig()
if envConfiguration.BaseURL != "" {
return NewConfiguration(envConfiguration)
}
localConfiguration := localConfig()
if localConfiguration.BaseURL != "" {
return NewConfiguration(localConfiguration)
}
homeConfiguration := homeConfig()
if homeConfiguration.BaseURL != "" {
fmt.Printf("Configuration file found in home directory, this approach of loading configuration will be deprecated in future releases, please upgrade the CLI and use the new 'sail sdk init config' command to create a local configuration file")
return NewConfiguration(homeConfiguration)
}
panic(fmt.Errorf("unable to find any config file"))
}