-
Notifications
You must be signed in to change notification settings - Fork 27
/
superkey.go
141 lines (114 loc) · 3.72 KB
/
superkey.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
package service
import (
"fmt"
"strconv"
"github.com/RedHatInsights/sources-api-go/config"
"github.com/RedHatInsights/sources-api-go/dao"
"github.com/RedHatInsights/sources-api-go/kafka"
l "github.com/RedHatInsights/sources-api-go/logger"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/redhatinsights/sources-superkey-worker/superkey"
)
const superkeyRequestedTopic = "platform.sources.superkey-requests"
var superkeyTopic = config.Get().KafkaTopic(superkeyRequestedTopic)
func SendSuperKeyCreateRequest(application *m.Application, headers []kafka.Header) error {
// load up the app + associations from the db+vault
application, err := loadApplication(application)
if err != nil {
return err
}
// fetch the metadata and transform it
steps, err := getApplicationSuperkeyMetaData(application)
if err != nil {
return err
}
// fetch the provider name from the static cache
provider := dao.Static.GetSourceTypeName(application.Source.SourceTypeID)
// fetch the extra values for this superkey request based on the provider type
extra, err := getExtraValues(application, provider)
if err != nil {
return err
}
// fetch the superkey authentication
superKey, err := getSuperKeyAuthentication(application)
if err != nil {
return err
}
superKeyId := superKey.GetID()
req := superkey.CreateRequest{
TenantID: application.Tenant.ExternalTenant,
SourceID: strconv.FormatInt(application.SourceID, 10),
ApplicationID: strconv.FormatInt(application.ID, 10),
ApplicationType: dao.Static.GetApplicationTypeName(application.ApplicationTypeID),
SuperKey: superKeyId,
Provider: provider,
Extra: extra,
SuperKeySteps: steps,
}
m := kafka.Message{}
err = m.AddValueAsJSON(&req)
if err != nil {
return err
}
m.AddHeaders(append(headers, kafka.Header{Key: "event_type", Value: []byte("create_application")}))
return produceSuperkeyRequest(&m)
}
func SendSuperKeyDeleteRequest(application *m.Application, headers []kafka.Header) error {
// load up the app + associations from the db+vault
application, err := loadApplication(application)
if err != nil {
return err
}
// fetch the metadata and transform it
steps, err := getApplicationSuperkeyMetaData(application)
if err != nil {
return err
}
// grab the authentication required for hitting the superkey provider
superKey, err := getSuperKeyAuthentication(application)
if err != nil {
l.Log.Warnf("SuperKey Authentication was nil - cleaning up incomplete superkey")
return nil
}
// parse out the existing data, we need to know the resource names to delete
skData, err := parseSuperKeyData(application.SuperkeyData)
if err != nil {
return err
}
if skData == nil {
l.Log.Warnf("SuperKey Data was nil - cleaning up incomplete superkey")
return nil
}
superKeyId := superKey.GetID()
req := superkey.DestroyRequest{
TenantID: application.Tenant.ExternalTenant,
SuperKey: superKeyId,
GUID: skData.GUID,
Provider: skData.Provider,
StepsCompleted: skData.StepsCompleted,
SuperKeySteps: steps,
}
m := kafka.Message{}
err = m.AddValueAsJSON(&req)
if err != nil {
return err
}
m.AddHeaders(append(headers, kafka.Header{Key: "event_type", Value: []byte("destroy_application")}))
return produceSuperkeyRequest(&m)
}
func produceSuperkeyRequest(m *kafka.Message) error {
writer, err := kafka.GetWriter(&kafka.Options{
BrokerConfig: conf.KafkaBrokerConfig,
Topic: superkeyTopic,
Logger: l.Log,
})
if err != nil {
return fmt.Errorf(`unable to create a Kafka writer to produce a superkey request: %w`, err)
}
defer kafka.CloseWriter(writer, "produce superkey request")
err = kafka.Produce(writer, m)
if err != nil {
return err
}
return nil
}