-
Notifications
You must be signed in to change notification settings - Fork 124
/
pod.go
132 lines (115 loc) · 3.63 KB
/
pod.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
package data
import (
"fmt"
"net/http"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/cloud"
"github.com/evergreen-ci/evergreen/model/pod"
"github.com/evergreen-ci/evergreen/rest/model"
"github.com/evergreen-ci/gimlet"
"github.com/evergreen-ci/utility"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
)
// CreatePod creates a new pod from the given REST model and returns its ID.
func CreatePod(apiPod model.APICreatePod) (*model.APICreatePodResponse, error) {
if apiPod.PodSecretValue == nil {
env := evergreen.GetEnvironment()
ctx, cancel := env.Context()
defer cancel()
smClient, err := cloud.MakeSecretsManagerClient(ctx, env.Settings())
if err != nil {
return nil, errors.Wrap(err, "getting Secrets Manager client")
}
defer smClient.Close(ctx)
v, err := cloud.MakeSecretsManagerVault(smClient)
if err != nil {
return nil, errors.Wrap(err, "initializing Secrets Manager vault")
}
podSecret, err := v.GetValue(ctx, utility.FromStringPtr(apiPod.PodSecretExternalID))
if err != nil {
return nil, errors.Wrap(err, "getting pod secret value")
}
apiPod.PodSecretValue = utility.ToStringPtr(podSecret)
}
dbPod, err := apiPod.ToService()
if err != nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: errors.Wrap(err, "converting pod to service model").Error(),
}
}
if err := dbPod.Insert(); err != nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: errors.Wrapf(err, "inserting new intent pod '%s'", dbPod.ID).Error(),
}
}
return &model.APICreatePodResponse{ID: dbPod.ID}, nil
}
// CheckPodSecret checks for a pod with a matching ID and secret in the
// database. It returns an error if the secret does not match the one assigned
// to the pod.
func CheckPodSecret(id, secret string) error {
p, err := FindPodByID(id)
if err != nil {
return gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: errors.Wrapf(err, "finding pod '%s'", id).Error(),
}
}
s, err := p.GetSecret()
if err != nil {
return gimlet.ErrorResponse{
StatusCode: http.StatusUnauthorized,
Message: errors.Wrap(err, "getting pod secret").Error(),
}
}
if secret != s.Value {
return gimlet.ErrorResponse{
StatusCode: http.StatusUnauthorized,
Message: "pod secrets do not match",
}
}
// The pod just successfully authed into the app server, so bump its last
// communicated time.
grip.Warning(message.WrapError(p.UpdateLastCommunicated(), message.Fields{
"message": "failed to update last communication time",
"pod": p.ID,
}))
return nil
}
// FindPodByID finds the pod by the given ID.
func FindPodByID(podID string) (*pod.Pod, error) {
p, err := pod.FindOneByID(podID)
if err != nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: errors.Wrapf(err, "finding pod '%s'", podID).Error(),
}
}
if p == nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusNotFound,
Message: fmt.Sprintf("pod '%s' not found", podID),
}
}
return p, nil
}
// FindAPIPodByID finds a pod by the given ID and returns its equivalent API model.
// It returns a nil result if no such pod is found.
func FindAPIPodByID(id string) (*model.APIPod, error) {
p, err := FindPodByID(id)
if err != nil {
return nil, err
}
var apiPod model.APIPod
if err := apiPod.BuildFromService(p); err != nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: errors.Wrap(err, "converting pod to API model").Error(),
}
}
return &apiPod, nil
}