-
Notifications
You must be signed in to change notification settings - Fork 27
/
superkey_helpers.go
160 lines (134 loc) · 4.56 KB
/
superkey_helpers.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
package service
import (
"encoding/json"
"fmt"
"github.com/RedHatInsights/sources-api-go/dao"
l "github.com/RedHatInsights/sources-api-go/logger"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/google/uuid"
"github.com/redhatinsights/sources-superkey-worker/superkey"
"gorm.io/datatypes"
)
// loads up the application as well as the associates we need for the superkey
// request
func loadApplication(application *m.Application) (*m.Application, error) {
appDao := dao.GetApplicationDao(&dao.RequestParams{TenantID: &application.TenantID})
// re-pulling it from the db to make sure we have the full-version, as well
// as preloading any relations necessary.
app, err := appDao.GetByIdWithPreload(&application.ID, "Source", "Source.Tenant", "Tenant")
if err != nil {
return nil, err
}
return app, nil
}
// returns the superkey steps from the metadata table for the specific
// application type
func getApplicationSuperkeyMetaData(application *m.Application) ([]superkey.Step, error) {
// fetch the metadata from the db (no tenancy required)
mDB := dao.GetMetaDataDao()
metadata, err := mDB.GetSuperKeySteps(application.ApplicationTypeID)
if err != nil {
return nil, err
}
steps := make([]superkey.Step, len(metadata))
// parse the data brought back from the db into the superkey "step" struct
for i, step := range metadata {
substitutions := make(map[string]string)
err := json.Unmarshal(step.Substitutions, &substitutions)
if err != nil {
return nil, err
}
steps[i] = superkey.Step{
Step: step.Step,
Name: step.Name,
Payload: string(step.Payload),
Substitutions: substitutions,
}
}
return steps, nil
}
// returns any extra values for the superkey provider
func getExtraValues(application *m.Application, provider string) (map[string]string, error) {
extra := make(map[string]string)
switch provider {
case "amazon":
// fetch the account number for replacing in the iam payloads
mDB := dao.GetMetaDataDao()
acct, err := mDB.GetSuperKeyAccountNumber(application.ApplicationTypeID)
if err != nil {
return nil, err
}
extra["account"] = acct
// fetch the result_type for the application_type
atDB := dao.GetApplicationTypeDao(nil)
authType, err := atDB.GetSuperKeyResultType(application.ApplicationTypeID, provider)
if err != nil {
return nil, err
}
extra["result_type"] = authType
externalID := uuid.New().String()
extra["external_id"] = externalID
default:
return nil, fmt.Errorf("invalid provider for superkey %v", provider)
}
return extra, nil
}
// returns the "super key" e.g. the authentication used to communicate with the
// provider
func getSuperKeyAuthentication(application *m.Application) (*m.Authentication, error) {
authDao := dao.GetAuthenticationDao(&dao.RequestParams{TenantID: &application.TenantID})
// fetch auths for this source
auths, _, err := authDao.ListForSource(application.SourceID, 100, 0, nil)
if err != nil {
return nil, err
}
// loop through, finding the source that is "attached" to the application's
// source and has the right authtype for superkey. This will need to be
// updated if we ever do superkey for other cloud types/authtypes
for i, auth := range auths {
// TODO: parameterize this if we need superkey on something OTHER than amazon.
if auth.ResourceID == application.SourceID && auth.AuthType == "access_key_secret_key" {
return &auths[i], nil
}
}
return nil, fmt.Errorf("superkey authentication not found")
}
type superKeyData struct {
GUID string
Provider string
StepsCompleted map[string]map[string]string
}
func parseSuperKeyData(data datatypes.JSON) (*superKeyData, error) {
if len(data) == 0 {
return nil, nil
}
superkeyData := make(map[string]interface{})
err := json.Unmarshal(data, &superkeyData)
if err != nil {
return nil, err
}
guid, ok := superkeyData["guid"].(string)
if !ok {
return nil, fmt.Errorf("invalid type for guid %v", superkeyData["guid"])
}
provider, ok := superkeyData["provider"].(string)
if !ok {
return nil, fmt.Errorf("invalid type for provider %v", superkeyData["provider"])
}
var stepsCompleted map[string]map[string]string
rawSteps := superkeyData["steps"]
l.Log.Debugf("rawSteps: %v", rawSteps)
if rawSteps != nil {
b, _ := json.Marshal(&rawSteps)
err := json.Unmarshal(b, &stepsCompleted)
if err != nil {
l.Log.Warnf("Failed to unmarshal completed steps into map: %v", err)
}
l.Log.Debugf("Found stepsCompleted: %v", stepsCompleted)
}
return &superKeyData{
GUID: guid,
Provider: provider,
StepsCompleted: stepsCompleted,
}, nil
}