@@ -11,11 +11,11 @@ import (
11
11
"gopkg.in/yaml.v2"
12
12
)
13
13
14
+ // configuration default values
14
15
const (
15
16
AuthenticationSchemeDefault string = string (types .AuthSchemeNative )
16
- AuthenticationFileDefault string = "~/.irods/.irodsA"
17
17
ClientServerNegotiationDefault string = string (types .CSNegotiationOff )
18
- ClientServerPolicyDefault string = string (types .CSNegotiationPolicyRequestDontCare )
18
+ ClientServerPolicyDefault string = string (types .CSNegotiationPolicyRequestTCP )
19
19
PortDefault int = 1247
20
20
HashSchemeDefault string = types .HashSchemeDefault
21
21
EncryptionAlgorithmDefault string = "AES-256-CBC"
@@ -25,8 +25,8 @@ const (
25
25
SSLVerifyServerDefault string = "hostname"
26
26
)
27
27
28
- // IRODSConfig stores irods config
29
- type IRODSConfig struct {
28
+ // Config stores irods config
29
+ type Config struct {
30
30
AuthenticationScheme string `json:"irods_authentication_scheme,omitempty" yaml:"irods_authentication_scheme,omitempty" envconfig:"IRODS_AUTHENTICATION_SCHEME"`
31
31
AuthenticationFile string `json:"irods_authentication_file,omitempty" yaml:"irods_authentication_file,omitempty" envconfig:"IRODS_AUTHENTICATION_FILE"`
32
32
ClientServerNegotiation string `json:"irods_client_server_negotiation,omitempty" yaml:"irods_client_server_negotiation,omitempty" envconfig:"IRODS_CLIENT_SERVER_NEGOTIATION"`
@@ -66,15 +66,10 @@ type IRODSConfig struct {
66
66
GSIServerDN string `json:"irods_gsi_server_dn,omitempty" yaml:"irods_gsi_server_dn,omitempty" envconfig:"IRODS_GSI_SERVER_DN"`
67
67
}
68
68
69
- func GetDefaultConfig () * IRODSConfig {
70
- authenticationFilePath , err := util .ExpandHomeDir (AuthenticationFileDefault )
71
- if err != nil {
72
- authenticationFilePath = ""
73
- }
74
-
75
- return & IRODSConfig {
69
+ func GetDefaultConfig () * Config {
70
+ return & Config {
76
71
AuthenticationScheme : AuthenticationSchemeDefault ,
77
- AuthenticationFile : authenticationFilePath ,
72
+ AuthenticationFile : GetDefaultPasswordFilePath () ,
78
73
ClientServerNegotiation : ClientServerNegotiationDefault ,
79
74
ClientServerPolicy : ClientServerPolicyDefault ,
80
75
Port : PortDefault ,
@@ -91,8 +86,8 @@ func GetDefaultConfig() *IRODSConfig {
91
86
}
92
87
}
93
88
94
- // NewConfigFromYAML creates Config from YAML
95
- func NewConfigFromYAML (yamlPath string ) (* IRODSConfig , error ) {
89
+ // NewConfigFromYamlFile creates Config from YAML
90
+ func NewConfigFromYamlFile (yamlPath string ) (* Config , error ) {
96
91
config := GetDefaultConfig ()
97
92
98
93
yamlBytes , err := os .ReadFile (yamlPath )
@@ -102,14 +97,14 @@ func NewConfigFromYAML(yamlPath string) (*IRODSConfig, error) {
102
97
103
98
err = yaml .Unmarshal (yamlBytes , config )
104
99
if err != nil {
105
- return nil , xerrors .Errorf ("failed to unmarshal %q to YAML : %w" , yamlPath , err )
100
+ return nil , xerrors .Errorf ("failed to unmarshal YAML file %q to config : %w" , yamlPath , err )
106
101
}
107
102
108
103
return config , nil
109
104
}
110
105
111
- // NewConfigFromJSON creates Config from JSON
112
- func NewConfigFromJSON (jsonPath string ) (* IRODSConfig , error ) {
106
+ // NewConfigFromJsonFile creates Config from JSON
107
+ func NewConfigFromJsonFile (jsonPath string ) (* Config , error ) {
113
108
config := GetDefaultConfig ()
114
109
115
110
jsonBytes , err := os .ReadFile (jsonPath )
@@ -119,14 +114,38 @@ func NewConfigFromJSON(jsonPath string) (*IRODSConfig, error) {
119
114
120
115
err = json .Unmarshal (jsonBytes , config )
121
116
if err != nil {
122
- return nil , xerrors .Errorf ("failed to unmarshal %q to JSON: %w" , jsonPath , err )
117
+ return nil , xerrors .Errorf ("failed to unmarshal JSON file %q to config: %w" , jsonPath , err )
118
+ }
119
+
120
+ return config , nil
121
+ }
122
+
123
+ // NewConfigFromJson creates Config from JSON
124
+ func NewConfigFromJson (jsonBytes []byte ) (* Config , error ) {
125
+ config := GetDefaultConfig ()
126
+
127
+ err := json .Unmarshal (jsonBytes , config )
128
+ if err != nil {
129
+ return nil , xerrors .Errorf ("failed to unmarshal JSON to Config: %w" , err )
130
+ }
131
+
132
+ return config , nil
133
+ }
134
+
135
+ // NewConfigFromEnv creates Config from Environmental variables
136
+ func NewConfigFromEnv () (* Config , error ) {
137
+ config := GetDefaultConfig ()
138
+
139
+ err := envconfig .Process ("" , config )
140
+ if err != nil {
141
+ return nil , xerrors .Errorf ("failed to read config from environmental variables: %w" , err )
123
142
}
124
143
125
144
return config , nil
126
145
}
127
146
128
- // NewConfigFromENV creates Config from Environmental variables
129
- func NewConfigFromENV (config * IRODSConfig ) (* IRODSConfig , error ) {
147
+ // OverwriteConfigFromEnv overwrites Config from Environmental variables
148
+ func OverwriteConfigFromEnv (config * Config ) (* Config , error ) {
130
149
if config == nil {
131
150
config = GetDefaultConfig ()
132
151
}
@@ -148,3 +167,82 @@ func GetDefaultIRODSConfigPath() string {
148
167
149
168
return irodsConfigPath
150
169
}
170
+
171
+ // ToIRODSAccount creates IRODSAccount
172
+ func (cfg * Config ) ToIRODSAccount () * types.IRODSAccount {
173
+ authScheme := types .GetAuthScheme (cfg .AuthenticationScheme )
174
+
175
+ negotiationPolicy , _ := types .GetCSNegotiationPolicyRequest (cfg .ClientServerPolicy )
176
+ negotiation , _ := types .GetCSNegotiation (cfg .ClientServerNegotiation )
177
+
178
+ verifyServer , _ := types .GetSSLVerifyServer (cfg .SSLVerifyServer )
179
+
180
+ account := & types.IRODSAccount {
181
+ AuthenticationScheme : authScheme ,
182
+ ClientServerNegotiation : negotiation .IsNegotiationRequired (),
183
+ CSNegotiationPolicy : negotiationPolicy ,
184
+ Host : cfg .Host ,
185
+ Port : cfg .Port ,
186
+ ClientUser : cfg .ClientUsername ,
187
+ ClientZone : cfg .ClientZoneName ,
188
+ ProxyUser : cfg .Username ,
189
+ ProxyZone : cfg .ZoneName ,
190
+ Password : cfg .Password ,
191
+ DefaultResource : cfg .DefaultResource ,
192
+ DefaultHashScheme : cfg .DefaultHashScheme ,
193
+ PamTTL : cfg .PAMTTL ,
194
+ PamToken : cfg .PAMToken ,
195
+ SSLConfiguration : & types.IRODSSSLConfig {
196
+ CACertificateFile : cfg .SSLCACertificateFile ,
197
+ CACertificatePath : cfg .SSLCACertificatePath ,
198
+ EncryptionKeySize : cfg .EncryptionKeySize ,
199
+ EncryptionAlgorithm : cfg .EncryptionAlgorithm ,
200
+ EncryptionSaltSize : cfg .EncryptionSaltSize ,
201
+ EncryptionNumHashRounds : cfg .EncryptionNumHashRounds ,
202
+ VerifyServer : verifyServer ,
203
+ DHParamsFile : cfg .SSLDHParamsFile ,
204
+ ServerName : cfg .SSLServerName ,
205
+ },
206
+ }
207
+
208
+ account .FixAuthConfiguration ()
209
+
210
+ return account
211
+ }
212
+
213
+ // ClearICommandsIncompatibleFields clears all icommands-incompatible fields
214
+ func (cfg * Config ) ClearICommandsIncompatibleFields () * Config {
215
+ cfg2 := * cfg
216
+
217
+ cfg2 .Password = ""
218
+ cfg2 .Ticket = ""
219
+ cfg2 .PAMToken = ""
220
+ cfg2 .PAMTTL = 0
221
+ cfg2 .SSLServerName = ""
222
+
223
+ return & cfg2
224
+ }
225
+
226
+ // ToJSON converts to JSON bytes
227
+ func (cfg * Config ) ToJSON () ([]byte , error ) {
228
+ jsonBytes , err := json .MarshalIndent (cfg , "" , " " )
229
+ if err != nil {
230
+ return nil , xerrors .Errorf ("failed to marshal configuration to json: %w" , err )
231
+ }
232
+
233
+ return jsonBytes , nil
234
+ }
235
+
236
+ // ToFile saves to a file
237
+ func (cfg * Config ) ToFile (envPath string ) error {
238
+ jsonByte , err := cfg .ToJSON ()
239
+ if err != nil {
240
+ return err
241
+ }
242
+
243
+ err = os .WriteFile (envPath , jsonByte , 0664 )
244
+ if err != nil {
245
+ return xerrors .Errorf ("failed to write to file %q: %w" , envPath , err )
246
+ }
247
+ return nil
248
+ }
0 commit comments