forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
144 lines (122 loc) · 4.44 KB
/
actions.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
package bitbucketcloud
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/pipeline/remote/model"
"github.com/rancher/rancher/pkg/ref"
v3 "github.com/rancher/types/apis/project.cattle.io/v3"
client "github.com/rancher/types/client/project/v3"
)
const (
bitbucketDefaultHostName = "https://bitbucket.org"
actionDisable = "disable"
actionTestAndApply = "testAndApply"
actionLogin = "login"
)
func (b *BcProvider) Formatter(apiContext *types.APIContext, resource *types.RawResource) {
if convert.ToBool(resource.Values["enabled"]) {
resource.AddAction(apiContext, actionDisable)
}
resource.AddAction(apiContext, actionTestAndApply)
}
func (b *BcProvider) ActionHandler(actionName string, action *types.Action, request *types.APIContext) error {
if actionName == actionTestAndApply {
return b.testAndApply(actionName, action, request)
} else if actionName == actionDisable {
return b.DisableAction(request, b.GetName())
}
return httperror.NewAPIError(httperror.ActionNotAvailable, "")
}
func (b *BcProvider) providerFormatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, actionLogin)
}
func (b *BcProvider) providerActionHandler(actionName string, action *types.Action, request *types.APIContext) error {
if actionName == actionLogin {
return b.authuser(request)
}
return httperror.NewAPIError(httperror.ActionNotAvailable, "")
}
func formBitbucketRedirectURLFromMap(config map[string]interface{}) string {
clientID := convert.ToString(config[client.BitbucketCloudPipelineConfigFieldClientID])
return fmt.Sprintf("%s/site/oauth2/authorize?client_id=%s&response_type=code", bitbucketDefaultHostName, clientID)
}
func (b *BcProvider) testAndApply(actionName string, action *types.Action, apiContext *types.APIContext) error {
applyInput := &v3.BitbucketCloudApplyInput{}
if err := json.NewDecoder(apiContext.Request.Body).Decode(applyInput); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent,
fmt.Sprintf("Failed to parse body: %v", err))
}
ns, _ := ref.Parse(apiContext.ID)
pConfig, err := b.GetProviderConfig(ns)
if err != nil {
return err
}
storedBitbucketPipelineConfig, ok := pConfig.(*v3.BitbucketCloudPipelineConfig)
if !ok {
return fmt.Errorf("Failed to get github provider config")
}
toUpdate := storedBitbucketPipelineConfig.DeepCopy()
toUpdate.ClientID = applyInput.ClientID
toUpdate.ClientSecret = applyInput.ClientSecret
toUpdate.RedirectURL = applyInput.RedirectURL
//oauth and add user
userName := apiContext.Request.Header.Get("Impersonate-User")
sourceCodeCredential, err := b.AuthAddAccount(userName, applyInput.Code, toUpdate, toUpdate.ProjectName, model.BitbucketCloudType)
if err != nil {
return err
}
if _, err = b.RefreshReposByCredentialAndConfig(sourceCodeCredential, toUpdate); err != nil {
return err
}
toUpdate.Enabled = true
//update bitbucket pipeline config
if _, err = b.SourceCodeProviderConfigs.ObjectClient().Update(toUpdate.Name, toUpdate); err != nil {
return err
}
apiContext.WriteResponse(http.StatusOK, nil)
return nil
}
func (b *BcProvider) authuser(apiContext *types.APIContext) error {
authUserInput := v3.AuthUserInput{}
requestBytes, err := ioutil.ReadAll(apiContext.Request.Body)
if err != nil {
return err
}
if err := json.Unmarshal(requestBytes, &authUserInput); err != nil {
return err
}
ns, _ := ref.Parse(apiContext.ID)
pConfig, err := b.GetProviderConfig(ns)
if err != nil {
return err
}
config, ok := pConfig.(*v3.BitbucketCloudPipelineConfig)
if !ok {
return fmt.Errorf("Failed to get bitbucket provider config")
}
if !config.Enabled {
return errors.New("bitbucket oauth app is not configured")
}
//oauth and add user
userName := apiContext.Request.Header.Get("Impersonate-User")
account, err := b.AuthAddAccount(userName, authUserInput.Code, config, config.ProjectName, model.BitbucketCloudType)
if err != nil {
return err
}
data := map[string]interface{}{}
if err := access.ByID(apiContext, apiContext.Version, client.SourceCodeCredentialType, account.Name, &data); err != nil {
return err
}
if _, err := b.RefreshReposByCredentialAndConfig(account, config); err != nil {
return err
}
apiContext.WriteResponse(http.StatusOK, data)
return nil
}