/
ipstack.go
124 lines (99 loc) · 3.12 KB
/
ipstack.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
package credential
import (
"context"
"os"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/modconfig"
"github.com/turbot/pipe-fittings/utils"
"github.com/zclconf/go-cty/cty"
)
type IPstackCredential struct {
CredentialImpl
AccessKey *string `json:"access_key,omitempty" cty:"access_key" hcl:"access_key,optional"`
}
func (c *IPstackCredential) getEnv() map[string]cty.Value {
return nil
}
func (c *IPstackCredential) CtyValue() (cty.Value, error) {
ctyValue, err := modconfig.GetCtyValue(c)
if err != nil {
return cty.NilVal, err
}
valueMap := ctyValue.AsValueMap()
valueMap["env"] = cty.ObjectVal(c.getEnv())
return cty.ObjectVal(valueMap), nil
}
func (c *IPstackCredential) Equals(otherCredential Credential) bool {
// If both pointers are nil, they are considered equal
if c == nil && helpers.IsNil(otherCredential) {
return true
}
if (c == nil && !helpers.IsNil(otherCredential)) || (c != nil && helpers.IsNil(otherCredential)) {
return false
}
other, ok := otherCredential.(*IPstackCredential)
if !ok {
return false
}
if !utils.PtrEqual(c.AccessKey, other.AccessKey) {
return false
}
return true
}
func (c *IPstackCredential) Resolve(ctx context.Context) (Credential, error) {
if c.AccessKey == nil {
// The order of precedence for the IPstack access key environment variable
// 1. IPSTACK_ACCESS_KEY
// 2. IPSTACK_TOKEN
ipstackAccessKeyEnvVar := os.Getenv("IPSTACK_TOKEN")
if os.Getenv("IPSTACK_ACCESS_KEY") != "" {
ipstackAccessKeyEnvVar = os.Getenv("IPSTACK_ACCESS_KEY")
}
// Don't modify existing credential, resolve to a new one
newCreds := &IPstackCredential{
CredentialImpl: c.CredentialImpl,
AccessKey: &ipstackAccessKeyEnvVar,
}
return newCreds, nil
}
return c, nil
}
func (c *IPstackCredential) GetTtl() int {
return -1
}
func (c *IPstackCredential) Validate() hcl.Diagnostics {
return hcl.Diagnostics{}
}
type IPStackConnectionConfig struct {
AccessKey *string `cty:"access_key" hcl:"access_key,optional"`
Https *bool `cty:"https" hcl:"https,optional"`
Security *bool `cty:"security" hcl:"security,optional"`
Token *string `cty:"token" hcl:"token,optional"`
}
func (c *IPStackConnectionConfig) GetCredential(name string, shortName string) Credential {
// Steampipe uses the attribute 'token' to configure the credential; whereas
// Flowpipe uses the attribute 'access_key' which is documented in the
// IPStack's official documentation: https://ipstack.com/documentation
// Since, Steampipe is still using the attribute 'token' we need a special handling in Flowpipe
// to support both 'access_key' and 'token', and the order of precedence will be 'access_key' and 'token'
var ipstackAccessKey string
if c.Token != nil {
ipstackAccessKey = *c.Token
}
if c.AccessKey != nil {
ipstackAccessKey = *c.AccessKey
}
ipstackCred := &IPstackCredential{
CredentialImpl: CredentialImpl{
HclResourceImpl: modconfig.HclResourceImpl{
FullName: name,
ShortName: shortName,
UnqualifiedName: name,
},
Type: "ipstack",
},
AccessKey: &ipstackAccessKey,
}
return ipstackCred
}