|
| 1 | +package aquasec |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aquasecurity/terraform-provider-aquasec/client" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func dataSourceAPIKeys() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Description: "Data source `aquasec_aqua_api_keys` provides all API keys by limit and offset.", |
| 17 | + ReadContext: dataAPIKeyRead, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "id": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Optional: true, |
| 22 | + ExactlyOneOf: []string{"id", "limit"}, |
| 23 | + }, |
| 24 | + "limit": { |
| 25 | + Type: schema.TypeInt, |
| 26 | + Optional: true, |
| 27 | + ExactlyOneOf: []string{"id", "limit"}, |
| 28 | + }, |
| 29 | + "offset": { |
| 30 | + Type: schema.TypeInt, |
| 31 | + Optional: true, |
| 32 | + }, |
| 33 | + "apikeys": { |
| 34 | + Type: schema.TypeList, |
| 35 | + Computed: true, |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "id": { |
| 39 | + Type: schema.TypeInt, |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + "access_key": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "description": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + "created": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "open_access": { |
| 55 | + Type: schema.TypeBool, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "scans_per_month": { |
| 59 | + Type: schema.TypeInt, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + "account_id": { |
| 63 | + Type: schema.TypeInt, |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + "enabled": { |
| 67 | + Type: schema.TypeBool, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "roles": { |
| 71 | + Type: schema.TypeList, |
| 72 | + Description: "The roles that will be assigned to the API key.", |
| 73 | + Computed: true, |
| 74 | + Elem: &schema.Schema{ |
| 75 | + Type: schema.TypeString, |
| 76 | + }, |
| 77 | + }, |
| 78 | + "group_id": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Description: "The group ID that is associated with the API key.", |
| 81 | + Computed: true, |
| 82 | + }, |
| 83 | + "expiration": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Description: "The date of the API key's expiry", |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + "permission_ids": { |
| 89 | + Type: schema.TypeList, |
| 90 | + Description: "List of permission IDs for the API key, if empty the API" + |
| 91 | + "key has global admin permissions.", |
| 92 | + Computed: true, |
| 93 | + Elem: &schema.Schema{ |
| 94 | + Type: schema.TypeInt, |
| 95 | + }, |
| 96 | + }, |
| 97 | + "ip_addresses": { |
| 98 | + Type: schema.TypeList, |
| 99 | + Description: "List of IP addresses the API key can be used from.", |
| 100 | + Computed: true, |
| 101 | + Elem: &schema.Schema{ |
| 102 | + Type: schema.TypeString, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func dataAPIKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 113 | + c := m.(*client.Client) |
| 114 | + var diags diag.Diagnostics |
| 115 | + |
| 116 | + if idRaw, ok := d.GetOk("id"); ok { |
| 117 | + id, err := strconv.Atoi(idRaw.(string)) |
| 118 | + if err != nil { |
| 119 | + return diag.FromErr(err) |
| 120 | + } |
| 121 | + key, err := c.GetApiKey(id) |
| 122 | + if err != nil { |
| 123 | + return diag.FromErr(err) |
| 124 | + } |
| 125 | + |
| 126 | + if key.ID == 0 { |
| 127 | + return diag.Errorf("API key returned with empty or zero id") |
| 128 | + } |
| 129 | + |
| 130 | + created := "" |
| 131 | + if key.CreatedAt != "" { |
| 132 | + if t, err := parseWithFallback(key.CreatedAt, time.RFC3339, time.Time{}); err == nil { |
| 133 | + created = t.Format(time.RFC3339) |
| 134 | + } else { |
| 135 | + log.Printf("[WARN] could not parse created %q: %v", key.CreatedAt, err) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + item := map[string]interface{}{ |
| 140 | + "id": key.ID, |
| 141 | + "access_key": key.AccessKey, |
| 142 | + "description": key.Description, |
| 143 | + "created": created, |
| 144 | + "open_access": key.OpenAccess, |
| 145 | + "scans_per_month": key.ScansPerMonth, |
| 146 | + "account_id": key.AccountID, |
| 147 | + "enabled": key.Enabled, |
| 148 | + "roles": key.Roles, |
| 149 | + "permission_ids": key.PermissionIDs, |
| 150 | + "expiration": key.Expiration, |
| 151 | + "group_id": key.GroupID, |
| 152 | + "ip_addresses": key.IPAddresses, |
| 153 | + } |
| 154 | + if err := d.Set("apikeys", []interface{}{item}); err != nil { |
| 155 | + return diag.FromErr(err) |
| 156 | + } |
| 157 | + d.SetId(strconv.Itoa(id)) |
| 158 | + return diags |
| 159 | + } |
| 160 | + if limitRaw, ok := d.GetOk("limit"); ok { |
| 161 | + limit := limitRaw.(int) |
| 162 | + offset := d.Get("offset").(int) |
| 163 | + |
| 164 | + if limit == 0 { |
| 165 | + return diag.Errorf("`limit` must be greater than 0 when using limit/offset") |
| 166 | + } |
| 167 | + |
| 168 | + keys, err := c.GetApiKeys(limit, offset) |
| 169 | + if err != nil { |
| 170 | + return diag.FromErr(err) |
| 171 | + } |
| 172 | + |
| 173 | + var items []map[string]interface{} |
| 174 | + for _, k := range keys { |
| 175 | + if k.ID == 0 { |
| 176 | + log.Printf("[WARN] skipping API key with empty id") |
| 177 | + continue |
| 178 | + } |
| 179 | + |
| 180 | + created := "" |
| 181 | + if k.CreatedAt != "" { |
| 182 | + if t, err := parseWithFallback(k.CreatedAt, time.RFC3339, time.Time{}); err == nil { |
| 183 | + created = t.Format(time.RFC3339) |
| 184 | + } else { |
| 185 | + log.Printf("[WARN] could not parse created %q: %v", k.CreatedAt, err) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + items = append(items, map[string]interface{}{ |
| 190 | + "id": k.ID, |
| 191 | + "access_key": k.AccessKey, |
| 192 | + "description": k.Description, |
| 193 | + "created": created, |
| 194 | + "open_access": k.OpenAccess, |
| 195 | + "scans_per_month": k.ScansPerMonth, |
| 196 | + "account_id": k.AccountID, |
| 197 | + "enabled": k.Enabled, |
| 198 | + "roles": k.Roles, |
| 199 | + "permission_ids": k.PermissionIDs, |
| 200 | + "expiration": k.Expiration, |
| 201 | + "group_id": k.GroupID, |
| 202 | + "ip_addresses": k.IPAddresses, |
| 203 | + }) |
| 204 | + } |
| 205 | + |
| 206 | + if err := d.Set("apikeys", items); err != nil { |
| 207 | + return diag.FromErr(err) |
| 208 | + } |
| 209 | + d.SetId(strconv.FormatInt(time.Now().Unix(), 10)) |
| 210 | + return diags |
| 211 | + } |
| 212 | + |
| 213 | + return diags |
| 214 | +} |
| 215 | + |
| 216 | +func parseWithFallback(s, layout string, fallback time.Time) (time.Time, error) { |
| 217 | + t, err := time.Parse(layout, s) |
| 218 | + if err != nil { |
| 219 | + return fallback, err |
| 220 | + } |
| 221 | + return t, nil |
| 222 | +} |
0 commit comments