/
abuseipdb.go
103 lines (80 loc) · 2.27 KB
/
abuseipdb.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
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 AbuseIPDBCredential struct {
CredentialImpl
APIKey *string `json:"api_key,omitempty" cty:"api_key" hcl:"api_key,optional"`
}
func (c *AbuseIPDBCredential) getEnv() map[string]cty.Value {
// There is no environment variable listed in the AbuseIPDB official API docs
// https://www.abuseipdb.com/api.html
return nil
}
func (c *AbuseIPDBCredential) 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 *AbuseIPDBCredential) 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.(*AbuseIPDBCredential)
if !ok {
return false
}
if !utils.PtrEqual(c.APIKey, other.APIKey) {
return false
}
return true
}
func (c *AbuseIPDBCredential) Resolve(ctx context.Context) (Credential, error) {
if c.APIKey == nil {
abuseIPDBAPIKeyEnvVar := os.Getenv("ABUSEIPDB_API_KEY")
// Don't modify existing credential, resolve to a new one
newCreds := &AbuseIPDBCredential{
CredentialImpl: c.CredentialImpl,
APIKey: &abuseIPDBAPIKeyEnvVar,
}
return newCreds, nil
}
return c, nil
}
func (c *AbuseIPDBCredential) GetTtl() int {
return -1
}
func (c *AbuseIPDBCredential) Validate() hcl.Diagnostics {
return hcl.Diagnostics{}
}
type AbuseIPDBConnectionConfig struct {
APIKey *string `cty:"api_key" hcl:"api_key"`
}
func (c *AbuseIPDBConnectionConfig) GetCredential(name string, shortName string) Credential {
abuseIPDBCred := &AbuseIPDBCredential{
CredentialImpl: CredentialImpl{
HclResourceImpl: modconfig.HclResourceImpl{
FullName: name,
ShortName: shortName,
UnqualifiedName: name,
},
Type: "abuseipdb",
},
APIKey: c.APIKey,
}
return abuseIPDBCred
}