-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_actions.go
92 lines (77 loc) · 2.92 KB
/
azure_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
package azure
import (
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
"github.com/rancher/norman/api/handler"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/auth/providers/common"
"github.com/rancher/types/apis/management.cattle.io/v3"
managementschema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
"github.com/rancher/types/apis/management.cattle.io/v3public"
"github.com/rancher/types/client/management/v3"
)
func (ap *azureProvider) formatter(apiContext *types.APIContext, resource *types.RawResource) {
common.AddCommonActions(apiContext, resource)
resource.AddAction(apiContext, "configureTest")
resource.AddAction(apiContext, "testAndApply")
}
func (ap *azureProvider) actionHandler(actionName string, action *types.Action, request *types.APIContext) error {
handled, err := common.HandleCommonAction(actionName, action, request, Name, ap.authConfigs)
if err != nil {
return err
}
if handled {
return nil
}
if actionName == "configureTest" {
return ap.configureTest(actionName, action, request)
} else if actionName == "testAndApply" {
return ap.testAndApply(actionName, action, request)
}
return httperror.NewAPIError(httperror.ActionNotAvailable, "")
}
func (ap *azureProvider) configureTest(actionName string, action *types.Action, request *types.APIContext) error {
// Verify the body has all required fields
input, err := handler.ParseAndValidateActionBody(request, request.Schemas.Schema(&managementschema.Version,
client.AzureADConfigType))
if err != nil {
return err
}
data := map[string]interface{}{
"redirectUrl": formAzureRedirectURL(input),
"type": "azureADConfigTestOutput",
}
request.WriteResponse(http.StatusOK, data)
return nil
}
func (ap *azureProvider) testAndApply(actionName string, action *types.Action, request *types.APIContext) error {
azureADConfigApplyInput := &v3.AzureADConfigApplyInput{}
if err := json.NewDecoder(request.Request.Body).Decode(azureADConfigApplyInput); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent,
fmt.Sprintf("Failed to parse body: %v", err))
}
azureADConfig := azureADConfigApplyInput.Config
azureLogin := &v3public.AzureADLogin{
Code: azureADConfigApplyInput.Code,
}
//Call provider
userPrincipal, groupPrincipals, providerInfo, err := ap.loginUser(azureLogin, &azureADConfig, true)
if err != nil {
if httperror.IsAPIError(err) {
return err
}
return errors.Wrap(err, "server error while authenticating")
}
user, err := ap.userMGR.SetPrincipalOnCurrentUser(request, userPrincipal)
if err != nil {
return err
}
err = ap.saveAzureConfigK8s(&azureADConfig)
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("Failed to save azure config: %v", err))
}
return ap.tokenMGR.CreateTokenAndSetCookie(user.Name, userPrincipal, groupPrincipals, providerInfo, 0, "Token via Azure Configuration", request)
}