@@ -16,9 +16,14 @@ import (
1616
1717// Config - godoc
1818type Config struct {
19- Username string `json:"tenant"`
20- Password string `json:"token"`
21- AquaURL string `json:"aqua_url"`
19+ Username string `json:"tenant"`
20+ Password string `json:"token"`
21+ AquaURL string `json:"aqua_url"`
22+ APIKey string `json:"aqua_api_key"`
23+ SecretKey string `json:"aqua_api_secret"`
24+ Validity int `json:"validity"`
25+ AllowedEndpoints []string `json:"allowed_endpoints"`
26+ CSPRoles []string `json:"csp_roles"`
2227}
2328
2429// Provider -
@@ -45,6 +50,20 @@ func Provider(v string) *schema.Provider {
4550 DefaultFunc : schema .EnvDefaultFunc ("AQUA_URL" , nil ),
4651 Description : "This is the base URL of your Aqua instance. Can alternatively be sourced from the `AQUA_URL` environment variable." ,
4752 },
53+ "aqua_api_key" : {
54+ Type : schema .TypeString ,
55+ Optional : true ,
56+ Sensitive : true ,
57+ DefaultFunc : schema .EnvDefaultFunc ("AQUA_API_KEY" , nil ),
58+ Description : "API key for authentication. If set, API key mode is used instead of token-based auth." ,
59+ },
60+ "aqua_api_secret" : {
61+ Type : schema .TypeString ,
62+ Optional : true ,
63+ Sensitive : true ,
64+ DefaultFunc : schema .EnvDefaultFunc ("AQUA_API_SECRET" , nil ),
65+ Description : "Shared secret for API key HMAC signing." ,
66+ },
4867 "verify_tls" : {
4968 Type : schema .TypeBool ,
5069 Optional : true ,
@@ -69,6 +88,27 @@ func Provider(v string) *schema.Provider {
6988 Default : true ,
7089 Description : "Skip provider credential validation when set to false." ,
7190 },
91+ "validity" : {
92+ Type : schema .TypeInt ,
93+ Optional : true ,
94+ Default : 240 ,
95+ Description : "Lifetime of the token, in minutes. Set between 1 and 1500. Once the token expires, need to generate a new one" ,
96+ },
97+ "allowed_endpoints" : {
98+ Type : schema .TypeList ,
99+ Optional : true ,
100+ Description : "API methods the token has access to" ,
101+ Elem : & schema.Schema {
102+ Type : schema .TypeString ,
103+ },
104+ },
105+ "csp_roles" : {
106+ Type : schema .TypeList ,
107+ Optional : true ,
108+ Elem : & schema.Schema {
109+ Type : schema .TypeString ,
110+ },
111+ },
72112 },
73113 ResourcesMap : map [string ]* schema.Resource {
74114 "aquasec_user" : resourceUser (),
@@ -143,23 +183,23 @@ func Provider(v string) *schema.Provider {
143183 }
144184}
145185
146- func getProviderConfigurationFromFile (d * schema.ResourceData ) (string , string , string , error ) {
186+ func getProviderConfigurationFromFile (d * schema.ResourceData ) (string , string , string , string , string , error ) {
147187 log .Print ("[DEBUG] Trying to load configuration from file" )
148188 if configPath , ok := d .GetOk ("config_path" ); ok && configPath .(string ) != "" {
149189 path , err := homedir .Expand (configPath .(string ))
150190 if err != nil {
151191 log .Printf ("[DEBUG] Failed to expand config file path %s, error %s" , configPath , err )
152- return "" , "" , "" , nil
192+ return "" , "" , "" , "" , "" , nil
153193 }
154194 if _ , err := os .Stat (path ); os .IsNotExist (err ) {
155195 log .Printf ("[DEBUG] Terraform config file %s does not exist, error %s" , path , err )
156- return "" , "" , "" , nil
196+ return "" , "" , "" , "" , "" , nil
157197 }
158198 log .Printf ("[DEBUG] Terraform configuration file is: %s" , path )
159199 configFile , err := os .Open (path )
160200 if err != nil {
161201 log .Printf ("[DEBUG] Unable to open Terraform configuration file %s" , path )
162- return "" , "" , "" , fmt .Errorf ("Unable to open terraform configuration file. Error %v" , err )
202+ return "" , "" , "" , "" , "" , fmt .Errorf ("Unable to open terraform configuration file. Error %v" , err )
163203 }
164204 defer configFile .Close ()
165205
@@ -168,11 +208,11 @@ func getProviderConfigurationFromFile(d *schema.ResourceData) (string, string, s
168208 err = json .Unmarshal (configBytes , & config )
169209 if err != nil {
170210 log .Printf ("[DEBUG] Failed to parse config file %s" , path )
171- return "" , "" , "" , fmt .Errorf ("Invalid terraform configuration file format. Error %v" , err )
211+ return "" , "" , "" , "" , "" , fmt .Errorf ("Invalid terraform configuration file format. Error %v" , err )
172212 }
173- return config .Username , config .Password , config .AquaURL , nil
213+ return config .Username , config .Password , config .AquaURL , config . APIKey , config . SecretKey , nil
174214 }
175- return "" , "" , "" , nil
215+ return "" , "" , "" , "" , "" , nil
176216}
177217
178218func providerConfigure (ctx context.Context , d * schema.ResourceData ) (interface {}, diag.Diagnostics ) {
@@ -183,32 +223,35 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
183223 username := d .Get ("username" ).(string )
184224 password := d .Get ("password" ).(string )
185225 aquaURL := d .Get ("aqua_url" ).(string )
226+ apiKey := d .Get ("aqua_api_key" ).(string )
227+ secretkey := d .Get ("aqua_api_secret" ).(string )
186228 verifyTLS := d .Get ("verify_tls" ).(bool )
187229 caCertPath := d .Get ("ca_certificate_path" ).(string )
188230 validate := d .Get ("validate" ).(bool )
189231
190- if username == "" && password == "" && aquaURL == "" {
191- username , password , aquaURL , err = getProviderConfigurationFromFile (d )
232+ if username == "" && password == "" && aquaURL == "" && apiKey == "" && secretkey == "" {
233+ username , password , aquaURL , apiKey , secretkey , err = getProviderConfigurationFromFile (d )
192234 if err != nil && validate {
193235 return nil , diag .FromErr (err )
194236 }
195237 }
196238
197239 if validate {
198- if username == "" {
199- diags = append (diags , diag.Diagnostic {
200- Severity : diag .Error ,
201- Summary : "Initializing provider, username parameter is missing." ,
202- })
203- }
240+ if apiKey == "" {
241+ if username == "" {
242+ diags = append (diags , diag.Diagnostic {
243+ Severity : diag .Error ,
244+ Summary : "Initializing provider, username parameter is missing." ,
245+ })
246+ }
204247
205- if password == "" {
206- diags = append (diags , diag.Diagnostic {
207- Severity : diag .Error ,
208- Summary : "Initializing provider, password parameter is missing." ,
209- })
248+ if password == "" {
249+ diags = append (diags , diag.Diagnostic {
250+ Severity : diag .Error ,
251+ Summary : "Initializing provider, password parameter is missing." ,
252+ })
253+ }
210254 }
211-
212255 if aquaURL == "" {
213256 diags = append (diags , diag.Diagnostic {
214257 Severity : diag .Error ,
@@ -235,7 +278,21 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
235278 return nil , diags
236279 }
237280
238- aquaClient := client .NewClient (aquaURL , username , password , verifyTLS , caCertByte )
281+ var aquaClient * client.Client
282+ if apiKey != "" {
283+ aquaClient = client .NewClientWithAPIKey (aquaURL , apiKey , secretkey , verifyTLS , caCertByte )
284+ if v , ok := d .GetOk ("validity" ); ok {
285+ aquaClient .Validity = v .(int )
286+ }
287+ if v , ok := d .GetOk ("allowed_endpoints" ); ok {
288+ aquaClient .AllowedEndpoints = convertStringArr (v .([]interface {}))
289+ }
290+ if v , ok := d .GetOk ("csp_roles" ); ok {
291+ aquaClient .CSPRoles = convertStringArr (v .([]interface {}))
292+ }
293+ } else {
294+ aquaClient = client .NewClientWithTokenAuth (aquaURL , username , password , verifyTLS , caCertByte )
295+ }
239296
240297 if validate {
241298 token , tokenPresent := os .LookupEnv ("TESTING_AUTH_TOKEN" )
0 commit comments