forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
117 lines (97 loc) · 2.94 KB
/
set.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
package teamserver
import (
"encoding/json"
"net/http"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/api/accessor"
"github.com/concourse/atc/api/present"
"github.com/concourse/skymarshal/provider"
)
func (s *Server) SetTeam(w http.ResponseWriter, r *http.Request) {
hLog := s.logger.Session("set-team")
hLog.Debug("setting-team")
acc := accessor.GetAccessor(r)
teamName := r.FormValue(":team_name")
var atcTeam atc.Team
err := json.NewDecoder(r.Body).Decode(&atcTeam)
if err != nil {
hLog.Error("malformed-request", err)
w.WriteHeader(http.StatusBadRequest)
return
}
atcTeam.Name = teamName
if !acc.IsAdmin() && !acc.IsAuthorized(teamName) {
w.WriteHeader(http.StatusForbidden)
return
}
providers := provider.GetProviders()
for providerName, config := range atcTeam.Auth {
p, found := providers[providerName]
if !found {
hLog.Error("failed-to-find-provider", err, lager.Data{"provider": providerName})
w.WriteHeader(http.StatusBadRequest)
return
}
authConfig, err := p.UnmarshalConfig(config)
if err != nil {
hLog.Error("failed-to-unmarshal-auth", err, lager.Data{"provider": providerName})
w.WriteHeader(http.StatusBadRequest)
return
}
err = authConfig.Validate()
if err != nil {
hLog.Error("request-body-validation-error", err, lager.Data{"provider": providerName})
w.WriteHeader(http.StatusBadRequest)
return
}
err = authConfig.Finalize()
if err != nil {
hLog.Error("cannot-finalize-auth-config", err, lager.Data{"provider": providerName})
w.WriteHeader(http.StatusBadRequest)
return
}
jsonConfig, err := p.MarshalConfig(authConfig)
if err != nil {
hLog.Error("cannot-marshal-auth-config", err, lager.Data{"provider": providerName})
w.WriteHeader(http.StatusBadRequest)
return
}
atcTeam.Auth[providerName] = jsonConfig
}
team, found, err := s.teamFactory.FindTeam(teamName)
if err != nil {
hLog.Error("failed-to-lookup-team", err, lager.Data{"teamName": teamName})
w.WriteHeader(http.StatusInternalServerError)
return
}
if found {
hLog.Debug("updating-credentials")
err = team.UpdateProviderAuth(atcTeam.Auth)
if err != nil {
hLog.Error("failed-to-update-team", err, lager.Data{"teamName": teamName})
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
} else if acc.IsAdmin() {
hLog.Debug("creating team")
team, err = s.teamFactory.CreateTeam(atcTeam)
if err != nil {
hLog.Error("failed-to-save-team", err, lager.Data{"teamName": teamName})
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
} else {
w.WriteHeader(http.StatusForbidden)
return
}
err = json.NewEncoder(w).Encode(present.Team(team))
if err != nil {
hLog.Error("failed-to-encode-team", err)
w.WriteHeader(http.StatusInternalServerError)
}
}