Skip to content

Commit 07ce5dd

Browse files
authored
Merge pull request #105 from pp511/PWX-26307-use-default-service-account-as-kms-account-when-device-level-account-info-not-specified
PWX-26307: Use default gce account when device level account not prov…
2 parents 727aa64 + bd43ea6 commit 07ce5dd

22 files changed

Lines changed: 1547 additions & 50 deletions

gce/gce.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gce
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"net/http"
78
"os"
@@ -19,6 +20,7 @@ import (
1920
"github.com/libopenstorage/openstorage/pkg/parser"
2021
"github.com/portworx/sched-ops/task"
2122
"github.com/sirupsen/logrus"
23+
google "golang.org/x/oauth2/google"
2224
compute "google.golang.org/api/compute/v1"
2325
container "google.golang.org/api/container/v1"
2426
"google.golang.org/api/googleapi"
@@ -63,6 +65,7 @@ type instance struct {
6365
clusterName string
6466
clusterLocation string
6567
nodePoolID string
68+
serviceAccount string
6669
}
6770

6871
// IsDevMode checks if the pkg is invoked in developer mode where GCE credentials
@@ -77,9 +80,10 @@ func IsDevMode() bool {
7780
func NewClient() (cloudops.Ops, error) {
7881

7982
var i = new(instance)
83+
ctx := context.Background()
8084
var err error
8185
if metadata.OnGCE() {
82-
err = gceInfo(i)
86+
err = gceInfo(ctx, i)
8387
} else if ok := IsDevMode(); ok {
8488
err = gceInfoFromEnv(i)
8589
} else {
@@ -90,7 +94,6 @@ func NewClient() (cloudops.Ops, error) {
9094
return nil, fmt.Errorf("error fetching instance info. Err: %v", err)
9195
}
9296

93-
ctx := context.Background()
9497
computeService, err := compute.NewService(ctx, option.WithScopes(compute.ComputeScope))
9598
if err != nil {
9699
return nil, fmt.Errorf("unable to create Compute service: %v", err)
@@ -378,6 +381,11 @@ func (s *gceOps) Create(
378381
"Invalid volume template given", "")
379382
}
380383

384+
if isDiskEncryptedWithDefaultAccount(v) {
385+
logrus.Infof("Default service account to be used as disk encryption kms service account")
386+
v.DiskEncryptionKey.KmsKeyServiceAccount = s.inst.serviceAccount
387+
}
388+
381389
newDisk := &compute.Disk{
382390
Description: "Disk created by openstorage",
383391
Labels: formatLabels(labels),
@@ -1173,7 +1181,7 @@ func (s *gceOps) describeinstance() (*compute.Instance, error) {
11731181
}
11741182

11751183
// gceInfo fetches the GCE instance metadata from the metadata server
1176-
func gceInfo(inst *instance) error {
1184+
func gceInfo(ctx context.Context, inst *instance) error {
11771185
var err error
11781186
inst.zone, err = metadata.Zone()
11791187
if err != nil {
@@ -1225,6 +1233,19 @@ func gceInfo(inst *instance) error {
12251233
}
12261234
}
12271235
}
1236+
1237+
credential, err := google.FindDefaultCredentials(ctx)
1238+
content := map[string]interface{}{}
1239+
json.Unmarshal(credential.JSON, &content)
1240+
if content["client_email"] != nil {
1241+
inst.serviceAccount = fmt.Sprintf("%s", content["client_email"])
1242+
} else {
1243+
serviceAccount, err := metadata.Email("")
1244+
if err != nil {
1245+
return fmt.Errorf("unable to get gce instance service account")
1246+
}
1247+
inst.serviceAccount = serviceAccount
1248+
}
12281249
return nil
12291250
}
12301251

@@ -1250,6 +1271,7 @@ func gceInfoFromEnv(inst *instance) error {
12501271
inst.clusterName, _ = cloudops.GetEnvValueStrict("GKE_CLUSTER_NAME")
12511272
inst.clusterLocation, _ = cloudops.GetEnvValueStrict("GKE_CLUSTER_LOCATION")
12521273
inst.nodePoolID, _ = cloudops.GetEnvValueStrict("GKE_NODE_POOL")
1274+
inst.serviceAccount, _ = cloudops.GetEnvValueStrict("GKE_CLUSTER_SERVICE_ACCOUNT")
12531275

12541276
return nil
12551277
}
@@ -1482,3 +1504,9 @@ func isZonalCluster(clusterLocation string) (bool, error) {
14821504
zoneRegex := "[a-zA-z0-9]+-[a-zA-Z0-9]+-[a-zA-Z]"
14831505
return regexp.MatchString(zoneRegex, clusterLocation)
14841506
}
1507+
1508+
func isDiskEncryptedWithDefaultAccount(d *compute.Disk) bool {
1509+
return d.DiskEncryptionKey != nil &&
1510+
len(d.DiskEncryptionKey.KmsKeyName) > 0 &&
1511+
len(d.DiskEncryptionKey.KmsKeyServiceAccount) == 0
1512+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/sirupsen/logrus v1.8.1
2424
github.com/stretchr/testify v1.7.0
2525
github.com/vmware/govmomi v0.22.2
26+
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
2627
google.golang.org/api v0.30.0
2728
gopkg.in/yaml.v2 v2.4.0
2829
k8s.io/apimachinery v0.20.4

go.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,7 @@ golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwY
15911591
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
15921592
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
15931593
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
1594+
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
15941595
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
15951596
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
15961597
golang.org/x/oauth2 v0.0.0-20180724155351-3d292e4d0cdc/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -1604,8 +1605,9 @@ golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a/go.mod h1:gOpvHmFTYa4Iltr
16041605
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
16051606
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
16061607
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
1607-
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5 h1:Lm4OryKCca1vehdsWogr9N4t7NfZxLbJoc/H0w4K4S4=
16081608
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
1609+
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 h1:OSnWWcOd/CtWQC2cYSBgbTSJv3ciqd8r54ySIW2y3RE=
1610+
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
16091611
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
16101612
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
16111613
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

vendor/golang.org/x/oauth2/authhandler/authhandler.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/oauth2/go.mod

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/oauth2/go.sum

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/oauth2/google/appengine_gen1.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/oauth2/google/appengine_gen2_flex.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)