Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webapp: Don't use DM for IAM. #1550

Merged
merged 3 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 132 additions & 1 deletion bootstrap/cmd/bootstrap/app/gcpUtils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package app

import (
"fmt"
"time"

"golang.org/x/net/context"
"google.golang.org/api/deploymentmanager/v2"
"golang.org/x/oauth2"
"github.com/ghodss/yaml"
log "github.com/sirupsen/logrus"
"google.golang.org/api/cloudresourcemanager/v1"
"io/ioutil"
"path"
)
Expand All @@ -20,6 +25,23 @@ type DmConf struct {
Resources []Resource `json:"resources"`
}

type IamBinding struct {
Members []string `type:"members`
Roles []string `type:"roles"`
}

type IamConf struct {
IamBindings []IamBinding `json:"bindings"`
}

type ApplyIamRequest struct {
Project string `json:"project"`
Cluster string `json:"cluster"`
Email string `json:"email"`
Token string `json:"token"`
Action string `json:"action`
}

// TODO: handle concurrent & repetitive deployment requests.
func (s *ksServer)InsertDeployment(ctx context.Context, req CreateRequest) error {
regPath := s.knownRegistries["kubeflow"].RegUri
Expand Down Expand Up @@ -83,4 +105,113 @@ func (s *ksServer)GetDeploymentStatus(ctx context.Context, req CreateRequest) (s
return "", err
}
return dm.Operation.Status, nil
}
}

func GetUpdatedPolicy(currentPolicy *cloudresourcemanager.Policy, iamConf *IamConf, req ApplyIamRequest) cloudresourcemanager.Policy {
// map from role to members.
policyMap := map[string]map[string]bool {}
for _, binding := range currentPolicy.Bindings {
policyMap[binding.Role] = make(map[string]bool)
for _, member := range binding.Members {
policyMap[binding.Role][member] = true
}
}

// Replace placeholder with actual identity.
saMapping := map[string]string {
"set-kubeflow-admin-service-account": fmt.Sprintf("serviceAccount:%v-admin@%v.iam.gserviceaccount.com", req.Cluster, req.Project),
"set-kubeflow-user-service-account": fmt.Sprintf("serviceAccount:%v-user@%v.iam.gserviceaccount.com", req.Cluster, req.Project),
"set-kubeflow-vm-service-account": fmt.Sprintf("serviceAccount:%v-vm@%v.iam.gserviceaccount.com", req.Cluster, req.Project),
"set-kubeflow-iap-account": fmt.Sprintf("user:%v", req.Email),
}
for _, binding := range iamConf.IamBindings {
for _, member := range binding.Members {
actualMember := member
if val, ok := saMapping[member]; ok {
actualMember = val
}
for _, role := range binding.Roles {
if req.Action == "add" {
policyMap[role][actualMember] = true
} else {
// action == "remove"
policyMap[role][actualMember] = false
}
}
}
}
newPolicy := cloudresourcemanager.Policy{}
for role, memberSet := range policyMap {
binding := cloudresourcemanager.Binding{}
binding.Role = role
for member, exists := range memberSet {
if exists {
binding.Members = append(binding.Members, member)
}
}
newPolicy.Bindings = append(newPolicy.Bindings, &binding)
}
return newPolicy
}

func (s *ksServer)ApplyIamPolicy(ctx context.Context, req ApplyIamRequest) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add retry into ApplyIamPolicy.
There's a small chance that concurrent iam change might occur for same project.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, PTAL

ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: req.Token,
})
resourceManager, err := cloudresourcemanager.New(oauth2.NewClient(ctx, ts))
if err != nil {
log.Errorf("Cannot create resource manager client: %v", err)
return err
}
s.serverMux.Lock()
defer s.serverMux.Unlock()

// Get current policy
retry := 0
var saPolicy *cloudresourcemanager.Policy
for retry < 5 {
saPolicy, err = resourceManager.Projects.GetIamPolicy(
req.Project,
&cloudresourcemanager.GetIamPolicyRequest{
}).Do()
if err != nil {
retry += 1
time.Sleep(3 * time.Second)
}
}
if err != nil {
log.Errorf("Cannot get current policy: %v", err)
return err
}

// Get the iam change from config.
regPath := s.knownRegistries["kubeflow"].RegUri
templatePath := path.Join(regPath, "../components/gcp-click-to-deploy/src/configs/iam_bindings_template.yaml")
var iamConf IamConf
err = LoadConfig(templatePath, &iamConf)
if err != nil {
log.Errorf("Failed to load iam config: %v", err)
return err
}

// Get the updated policy and apply it.
newPolicy := GetUpdatedPolicy(saPolicy, &iamConf, req)
retry = 0
if retry < 5 {
_, err = resourceManager.Projects.SetIamPolicy(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the failure is caused by concurrent iam policy change, we need to re-get the changed iam policy as refresh start and then apply configs to it and try to set iam again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, updated. Thanks

req.Project,
&cloudresourcemanager.SetIamPolicyRequest{
Policy: &newPolicy,
}).Do()
if err != nil {
retry += 1
time.Sleep(3 * time.Second)
}
}
if err != nil {
log.Errorf("Cannot set new ploicy: %v", err)
return err
}

return nil
}
39 changes: 39 additions & 0 deletions bootstrap/cmd/bootstrap/app/ksServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type KsService interface {
BindRole(context.Context, string, string, string) error
InsertDeployment(context.Context, CreateRequest) error
GetDeploymentStatus(context.Context, CreateRequest) (string, error)
ApplyIamPolicy(context.Context, ApplyIamRequest) error
}

// appInfo keeps track of information about apps.
Expand Down Expand Up @@ -813,6 +814,19 @@ func finishDeployment(svc KsService, req CreateRequest) {
return
}

log.Info("Patching IAM bindings...")
err = svc.ApplyIamPolicy(ctx, ApplyIamRequest{
Project: req.Project,
Cluster: req.Cluster,
Email: req.Email,
Token: req.Token,
Action: "add",
})
if err != nil {
log.Errorf("Failed to update IAM: %v", err)
return
}

log.Infof("Inserting sa keys...")
err = svc.InsertSaKeys(ctx, InsertSaKeyRequest{
Cluster: req.Cluster,
Expand Down Expand Up @@ -897,6 +911,18 @@ func makeSaKeyEndpoint(svc KsService) endpoint.Endpoint {
}
}

func makeIamEndpoint(svc KsService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(ApplyIamRequest)
err := svc.ApplyIamPolicy(ctx, req)
r := &basicServerResponse{}
if err != nil {
r.Err = err.Error()
}
return r, nil
}
}

func decodeCreateAppRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request CreateRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
Expand Down Expand Up @@ -971,6 +997,18 @@ func (s *ksServer) StartHttp(port int) {
encodeResponse,
)

applyIamHandler := httptransport.NewServer(
makeIamEndpoint(s),
func (_ context.Context, r *http.Request) (interface{}, error) {
var request ApplyIamRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
},
encodeResponse,
)

initProjectHandler := httptransport.NewServer(
makeInitProjectEndpoint(s),
func (_ context.Context, r *http.Request) (interface{}, error) {
Expand All @@ -995,6 +1033,7 @@ func (s *ksServer) StartHttp(port int) {
http.Handle("/kfctl/apps/apply", optionsHandler(applyAppHandler))
http.Handle("/kfctl/apps/create", optionsHandler(createAppHandler))
http.Handle("/kfctl/iam/insertSaKey", optionsHandler(insertSaKeyHandler))
http.Handle("/kfctl/iam/apply", optionsHandler(applyIamHandler))
http.Handle("/kfctl/initProject", optionsHandler(initProjectHandler))
http.Handle("/kfctl/e2eDeploy", optionsHandler(deployHandler))

Expand Down
Loading