|
| 1 | +package clusterapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | + resourcemanager "google.golang.org/api/cloudresourcemanager/v1" |
| 10 | + iam "google.golang.org/api/iam/v1" |
| 11 | + "google.golang.org/api/option" |
| 12 | + |
| 13 | + gcp "github.com/openshift/installer/pkg/asset/installconfig/gcp" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + retryTime = 10 * time.Second |
| 18 | + retryCount = 6 |
| 19 | +) |
| 20 | + |
| 21 | +func defaultServiceAccountID(infraID, projectID, role string) string { |
| 22 | + // The account id is used to generate the service account email address, |
| 23 | + // it should not contain the email suffixi. It is unique within a project, |
| 24 | + // must be 6-30 characters long, and match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])` |
| 25 | + return fmt.Sprintf("%s-%s", infraID, role[0:1]) |
| 26 | +} |
| 27 | + |
| 28 | +// GetMasterRoles returns the pre-defined roles for a master node. |
| 29 | +// Roles are described here https://cloud.google.com/iam/docs/understanding-roles#predefined_roles. |
| 30 | +func GetMasterRoles() []string { |
| 31 | + return []string{ |
| 32 | + "roles/compute.instanceAdmin", |
| 33 | + "roles/compute.networkAdmin", |
| 34 | + "roles/compute.securityAdmin", |
| 35 | + "roles/storage.admin", |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// GetWorkerRoles returns the pre-defined roles for a worker node. |
| 40 | +func GetWorkerRoles() []string { |
| 41 | + return []string{ |
| 42 | + "roles/compute.viewer", |
| 43 | + "roles/storage.admin", |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// CreateServiceAccount is used to create a service account for a compute instance. |
| 48 | +func CreateServiceAccount(ctx context.Context, infraID, projectID, role string) (string, error) { |
| 49 | + ctx, cancel := context.WithTimeout(ctx, time.Minute*1) |
| 50 | + defer cancel() |
| 51 | + |
| 52 | + ssn, err := gcp.GetSession(ctx) |
| 53 | + if err != nil { |
| 54 | + return "", fmt.Errorf("failed to get session: %w", err) |
| 55 | + } |
| 56 | + service, err := iam.NewService(ctx, option.WithCredentials(ssn.Credentials)) |
| 57 | + if err != nil { |
| 58 | + return "", fmt.Errorf("failed to create IAM service: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + accountID := defaultServiceAccountID(infraID, projectID, role) |
| 62 | + displayName := fmt.Sprintf("%s-%s-node", infraID, role) |
| 63 | + |
| 64 | + request := &iam.CreateServiceAccountRequest{ |
| 65 | + AccountId: accountID, |
| 66 | + ServiceAccount: &iam.ServiceAccount{ |
| 67 | + Description: "The service account used by the instances.", |
| 68 | + DisplayName: displayName, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + sa, err := service.Projects.ServiceAccounts.Create("projects/"+projectID, request).Do() |
| 73 | + if err != nil { |
| 74 | + return "", fmt.Errorf("Projects.ServiceAccounts.Create: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + // Poll for service account |
| 78 | + for i := 0; i < retryCount; i++ { |
| 79 | + _, err := service.Projects.ServiceAccounts.Get(sa.Name).Do() |
| 80 | + if err == nil { |
| 81 | + logrus.Debugf("Service account created for %s", accountID) |
| 82 | + return accountID, nil |
| 83 | + } |
| 84 | + time.Sleep(retryTime) |
| 85 | + } |
| 86 | + |
| 87 | + return "", fmt.Errorf("failure creating service account: %w", err) |
| 88 | +} |
| 89 | + |
| 90 | +// AddServiceAccountRoles adds predefined roles for service account. |
| 91 | +func AddServiceAccountRoles(ctx context.Context, projectID, serviceAccountID string, roles []string) error { |
| 92 | + policy, err := getProjectIAMPolicy(ctx, projectID) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + for _, role := range roles { |
| 98 | + err = addMemberToRole(policy, role, serviceAccountID) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to add role %s to %s: %w", role, serviceAccountID, err) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func getProjectIAMPolicy(ctx context.Context, projectID string) (*resourcemanager.Policy, error) { |
| 108 | + ctx, cancel := context.WithTimeout(ctx, time.Minute*1) |
| 109 | + defer cancel() |
| 110 | + req := &resourcemanager.GetIamPolicyRequest{} |
| 111 | + |
| 112 | + ssn, err := gcp.GetSession(ctx) |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf("failed to get session: %w", err) |
| 115 | + } |
| 116 | + service, err := resourcemanager.NewService(ctx, option.WithCredentials(ssn.Credentials)) |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("failed to create resourcemanager service: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + policy, err := service.Projects.GetIamPolicy(projectID, req).Context(ctx).Do() |
| 122 | + if err != nil { |
| 123 | + return nil, fmt.Errorf("failed to fetch project IAM policy: %w", err) |
| 124 | + } |
| 125 | + return policy, nil |
| 126 | +} |
| 127 | + |
| 128 | +// addMemberToRole adds a member to a role binding. |
| 129 | +func addMemberToRole(policy *resourcemanager.Policy, role, member string) error { |
| 130 | + for _, binding := range policy.Bindings { |
| 131 | + if binding.Role == role { |
| 132 | + for _, m := range binding.Members { |
| 133 | + if m == member { |
| 134 | + logrus.Debugf("found %s role, member %s already exists", role, member) |
| 135 | + return nil |
| 136 | + } |
| 137 | + } |
| 138 | + binding.Members = append(binding.Members, member) |
| 139 | + logrus.Debugf("found %s role, added %s member", role, member) |
| 140 | + return nil |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return fmt.Errorf("role %s not found, member %s not added", role, member) |
| 145 | +} |
0 commit comments