-
Notifications
You must be signed in to change notification settings - Fork 3
/
flowpipe_credential.go
211 lines (179 loc) · 8.27 KB
/
flowpipe_credential.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package credential
import (
"context"
"log/slog"
"reflect"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/pipe-fittings/v2/error_helpers"
"github.com/turbot/pipe-fittings/v2/modconfig"
"github.com/turbot/pipe-fittings/v2/perr"
"github.com/zclconf/go-cty/cty"
)
var credentialTypeRegistry = map[string]reflect.Type{
"abuseipdb": reflect.TypeOf((*AbuseIPDBCredential)(nil)).Elem(),
"alicloud": reflect.TypeOf((*AlicloudCredential)(nil)).Elem(),
"aws": reflect.TypeOf((*AwsCredential)(nil)).Elem(),
"azure": reflect.TypeOf((*AzureCredential)(nil)).Elem(),
"bitbucket": reflect.TypeOf((*BitbucketCredential)(nil)).Elem(),
"clickup": reflect.TypeOf((*ClickUpCredential)(nil)).Elem(),
"datadog": reflect.TypeOf((*DatadogCredential)(nil)).Elem(),
"discord": reflect.TypeOf((*DiscordCredential)(nil)).Elem(),
"freshdesk": reflect.TypeOf((*FreshdeskCredential)(nil)).Elem(),
"gcp": reflect.TypeOf((*GcpCredential)(nil)).Elem(),
"github": reflect.TypeOf((*GithubCredential)(nil)).Elem(),
"gitlab": reflect.TypeOf((*GitLabCredential)(nil)).Elem(),
"guardrails": reflect.TypeOf((*GuardrailsCredential)(nil)).Elem(),
"ip2locationio": reflect.TypeOf((*IP2LocationIOCredential)(nil)).Elem(),
"ipstack": reflect.TypeOf((*IPstackCredential)(nil)).Elem(),
"jira": reflect.TypeOf((*JiraCredential)(nil)).Elem(),
"jumpcloud": reflect.TypeOf((*JumpCloudCredential)(nil)).Elem(),
"mastodon": reflect.TypeOf((*MastodonCredential)(nil)).Elem(),
"okta": reflect.TypeOf((*OktaCredential)(nil)).Elem(),
"openai": reflect.TypeOf((*OpenAICredential)(nil)).Elem(),
"opsgenie": reflect.TypeOf((*OpsgenieCredential)(nil)).Elem(),
"pagerduty": reflect.TypeOf((*PagerDutyCredential)(nil)).Elem(),
"pipes": reflect.TypeOf((*PipesCredential)(nil)).Elem(),
"sendgrid": reflect.TypeOf((*SendGridCredential)(nil)).Elem(),
"servicenow": reflect.TypeOf((*ServiceNowCredential)(nil)).Elem(),
"slack": reflect.TypeOf((*SlackCredential)(nil)).Elem(),
"teams": reflect.TypeOf((*MicrosoftTeamsCredential)(nil)).Elem(),
"trello": reflect.TypeOf((*TrelloCredential)(nil)).Elem(),
"uptimerobot": reflect.TypeOf((*UptimeRobotCredential)(nil)).Elem(),
"urlscan": reflect.TypeOf((*UrlscanCredential)(nil)).Elem(),
"vault": reflect.TypeOf((*VaultCredential)(nil)).Elem(),
"virustotal": reflect.TypeOf((*VirusTotalCredential)(nil)).Elem(),
"zendesk": reflect.TypeOf((*ZendeskCredential)(nil)).Elem(),
}
func instantiateCredential(key string, hclResourceImpl modconfig.HclResourceImpl) (Credential, error) {
t, exists := credentialTypeRegistry[key]
if !exists {
return nil, perr.BadRequestWithMessage("Invalid credential type " + key)
}
credInterface := reflect.New(t).Interface()
cred, ok := credInterface.(Credential)
if !ok {
return nil, perr.InternalWithMessage("Failed to create credential")
}
cred.SetHclResourceImpl(hclResourceImpl)
cred.SetCredentialType(key)
return cred, nil
}
var credentialConfigTypeRegistry = map[string]reflect.Type{
"abuseipdb": reflect.TypeOf((*AbuseIPDBConnectionConfig)(nil)).Elem(),
"alicloud": reflect.TypeOf((*AlicloudConnectionConfig)(nil)).Elem(),
"aws": reflect.TypeOf((*AwsConnectionConfig)(nil)).Elem(),
"azure": reflect.TypeOf((*AzureConnectionConfig)(nil)).Elem(),
"bitbucket": reflect.TypeOf((*BitbucketConnectionConfig)(nil)).Elem(),
"clickup": reflect.TypeOf((*ClickUpConnectionConfig)(nil)).Elem(),
"datadog": reflect.TypeOf((*DatadogConnectionConfig)(nil)).Elem(),
"discord": reflect.TypeOf((*DiscordConnectionConfig)(nil)).Elem(),
"freshdesk": reflect.TypeOf((*FreshdeskConnectionConfig)(nil)).Elem(),
"gcp": reflect.TypeOf((*GcpConnectionConfig)(nil)).Elem(),
"github": reflect.TypeOf((*GithubConnectionConfig)(nil)).Elem(),
"gitlab": reflect.TypeOf((*GitlabConnectionConfig)(nil)).Elem(),
"guardrails": reflect.TypeOf((*GuardrailsConnectionConfig)(nil)).Elem(),
"ip2locationio": reflect.TypeOf((*IP2LocationIOConnectionConfig)(nil)).Elem(),
"ipstack": reflect.TypeOf((*IPStackConnectionConfig)(nil)).Elem(),
"jira": reflect.TypeOf((*JiraConnectionConfig)(nil)).Elem(),
"jumpcloud": reflect.TypeOf((*JumpCloudConnectionConfig)(nil)).Elem(),
"mastodon": reflect.TypeOf((*MastodonConnectionConfig)(nil)).Elem(),
"okta": reflect.TypeOf((*OktaConnectionConfig)(nil)).Elem(),
"openai": reflect.TypeOf((*OpenAIConnectionConfig)(nil)).Elem(),
"opsgenie": reflect.TypeOf((*OpsgenieConnectionConfig)(nil)).Elem(),
"pagerduty": reflect.TypeOf((*PagerDutyConnectionConfig)(nil)).Elem(),
"pipes": reflect.TypeOf((*PipesConnectionConfig)(nil)).Elem(),
"sendgrid": reflect.TypeOf((*SendGridConnectionConfig)(nil)).Elem(),
"servicenow": reflect.TypeOf((*ServiceNowConnectionConfig)(nil)).Elem(),
"slack": reflect.TypeOf((*SlackConnectionConfig)(nil)).Elem(),
"teams": reflect.TypeOf((*MicrosoftTeamsConnectionConfig)(nil)).Elem(),
"trello": reflect.TypeOf((*TrelloConnectionConfig)(nil)).Elem(),
"uptimerobot": reflect.TypeOf((*UptimeRobotConnectionConfig)(nil)).Elem(),
"urlscan": reflect.TypeOf((*UrlscanConnectionConfig)(nil)).Elem(),
"vault": reflect.TypeOf((*VaultConnectionConfig)(nil)).Elem(),
"virustotal": reflect.TypeOf((*VirusTotalConnectionConfig)(nil)).Elem(),
"zendesk": reflect.TypeOf((*ZendeskConnectionConfig)(nil)).Elem(),
}
func InstantiateCredentialConfig(key string) (CredentialConfig, error) {
// If the key has a slash, extract the last part of the key
if strings.Contains(key, "/") {
strParts := strings.Split(key, "/")
key = strParts[len(strParts)-1]
}
t, exists := credentialConfigTypeRegistry[key]
if !exists {
// Currently the Flowpipe only supports very small set of credential types as compare to the Steampipe.
// To avoid the parse error, skip the credential types that are not supported by the Flowpipe.
slog.Error("Invalid credential type", "credential_type", key)
return nil, nil
}
credConfigInterface := reflect.New(t).Interface()
credConfig, ok := credConfigInterface.(CredentialConfig)
if !ok {
return nil, perr.InternalWithMessage("Failed to create credential config")
}
return credConfig, nil
}
func DefaultCredentials() (map[string]Credential, error) {
credentials := make(map[string]Credential)
for k := range credentialTypeRegistry {
hclResourceImpl := modconfig.HclResourceImpl{
FullName: k + ".default",
ShortName: "default",
UnqualifiedName: k + ".default",
}
defaultCred, err := instantiateCredential(k, hclResourceImpl)
if err != nil {
return nil, err
}
credentials[k+".default"] = defaultCred
error_helpers.RegisterCredentialType(k)
}
return credentials, nil
}
func NewCredential(block *hcl.Block) (Credential, error) {
credentialType := block.Labels[0]
credentialName := block.Labels[1]
hclResourceImpl := modconfig.NewHclResourceImplNoMod(block, credentialType, credentialName)
credential, err := instantiateCredential(credentialType, hclResourceImpl)
if err != nil {
return nil, err
}
return credential, err
}
type CredentialConfig interface {
GetCredential(string, string) Credential
}
type Credential interface {
modconfig.HclResource
modconfig.ResourceWithMetadata
SetHclResourceImpl(hclResourceImpl modconfig.HclResourceImpl)
GetCredentialType() string
SetCredentialType(string)
GetUnqualifiedName() string
CtyValue() (cty.Value, error)
Resolve(ctx context.Context) (Credential, error)
GetTtl() int // in seconds
Validate() hcl.Diagnostics
getEnv() map[string]cty.Value
Equals(Credential) bool
}
type CredentialImpl struct {
modconfig.HclResourceImpl
modconfig.ResourceWithMetadataImpl
// required to allow partial decoding
HclResourceRemain hcl.Body `hcl:",remain" json:"-"`
Type string `json:"type" cty:"type" hcl:"type,label"`
}
func (c *CredentialImpl) GetUnqualifiedName() string {
return c.HclResourceImpl.UnqualifiedName
}
func (c *CredentialImpl) SetHclResourceImpl(hclResourceImpl modconfig.HclResourceImpl) {
c.HclResourceImpl = hclResourceImpl
}
func (c *CredentialImpl) GetCredentialType() string {
return c.Type
}
func (c *CredentialImpl) SetCredentialType(credType string) {
c.Type = credType
}