Skip to content

Commit 58b28e0

Browse files
tamalsahaEmruz Hossain
andauthored
Install license handler to operator webhook server (#1194)
Signed-off-by: Tamal Saha <tamal@appscode.com> Co-authored-by: Emruz Hossain <emruz@appscode.com>
1 parent 642f935 commit 58b28e0

File tree

20 files changed

+838
-151
lines changed

20 files changed

+838
-151
lines changed

go.mod

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,17 @@ module stash.appscode.dev/stash
33
go 1.12
44

55
require (
6-
cloud.google.com/go v0.56.0 // indirect
76
github.com/Azure/azure-sdk-for-go v43.0.0+incompatible // indirect
8-
github.com/Azure/go-autorest/autorest v0.10.2 // indirect
9-
github.com/Azure/go-autorest/autorest/adal v0.8.3 // indirect
107
github.com/Azure/go-autorest/autorest/to v0.3.1-0.20191028180845-3492b2aff503 // indirect
118
github.com/appscode/go v0.0.0-20200323182826-54e98e09185a
129
github.com/aws/aws-sdk-go v1.31.9 // indirect
1310
github.com/cenkalti/backoff v2.2.1+incompatible
1411
github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27
15-
github.com/go-openapi/spec v0.19.7 // indirect
16-
github.com/go-openapi/swag v0.19.9 // indirect
1712
github.com/go-sql-driver/mysql v1.5.0
1813
github.com/gogo/protobuf v1.3.1
1914
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
20-
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 // indirect
21-
github.com/googleapis/gnostic v0.4.0 // indirect
22-
github.com/gophercloud/gophercloud v0.11.0 // indirect
2315
github.com/gopherjs/gopherjs v0.0.0-20191106031601-ce3c9ade29de // indirect
24-
github.com/grpc-ecosystem/grpc-gateway v1.14.6 // indirect
25-
github.com/hashicorp/golang-lru v0.5.4 // indirect
26-
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
27-
github.com/kr/pretty v0.2.0 // indirect
2816
github.com/kubernetes-csi/external-snapshotter/v2 v2.1.1-0.20200521091436-82ef6e66e992
29-
github.com/mailru/easyjson v0.7.1 // indirect
30-
github.com/mattn/go-colorable v0.1.6 // indirect
31-
github.com/mitchellh/mapstructure v1.2.2 // indirect
3217
github.com/onsi/ginkgo v1.11.0
3318
github.com/onsi/gomega v1.8.1
3419
github.com/opencontainers/go-digest v1.0.0 // indirect
@@ -40,29 +25,20 @@ require (
4025
github.com/spf13/afero v1.2.2
4126
github.com/spf13/cobra v0.0.5
4227
github.com/spf13/pflag v1.0.5
43-
go.bytebuilders.dev/license-verifier/kubernetes v0.2.2
28+
go.bytebuilders.dev/license-verifier/kubernetes v0.3.0
4429
go.opencensus.io v0.22.2 // indirect
45-
go.uber.org/atomic v1.6.0 // indirect
46-
go.uber.org/zap v1.13.0 // indirect
47-
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a // indirect
48-
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
49-
golang.org/x/tools v0.0.0-20200603131246-cc40288be839 // indirect
5030
gomodules.xyz/cert v1.0.3
5131
gomodules.xyz/envsubst v0.1.0
5232
gomodules.xyz/stow v0.2.3
5333
google.golang.org/api v0.26.0 // indirect
54-
google.golang.org/appengine v1.6.6 // indirect
55-
google.golang.org/genproto v0.0.0-20200603110839-e855014d5736 // indirect
5634
gopkg.in/ini.v1 v1.51.0
57-
gopkg.in/yaml.v2 v2.3.0 // indirect
5835
k8s.io/api v0.18.3
5936
k8s.io/apiextensions-apiserver v0.18.3
6037
k8s.io/apimachinery v0.18.8
6138
k8s.io/apiserver v0.18.3
6239
k8s.io/client-go v12.0.0+incompatible
6340
k8s.io/kube-aggregator v0.18.3
6441
k8s.io/kubernetes v1.18.3
65-
k8s.io/utils v0.0.0-20200414100711-2df71ebbae66 // indirect
6642
kmodules.xyz/client-go v0.0.0-20200915091229-7df16c29f4e8
6743
kmodules.xyz/constants v0.0.0-20200506032633-a21e58ceec72
6844
kmodules.xyz/custom-resources v0.0.0-20200604135349-9e9f5c4fdba9
@@ -71,11 +47,9 @@ require (
7147
kmodules.xyz/openshift v0.0.0-20200522123204-ce4abf5433c8
7248
kmodules.xyz/prober v0.0.0-20200521101241-adf06150535c
7349
kmodules.xyz/webhook-runtime v0.0.0-20200522123600-ca70a7e28ed0
74-
stash.appscode.dev/apimachinery v0.10.1-0.20200914045248-546ceea96940
50+
stash.appscode.dev/apimachinery v0.10.1-0.20200916073701-0189ba363808
7551
)
7652

77-
// release-1.18
78-
7953
replace bitbucket.org/ww/goautoneg => gomodules.xyz/goautoneg v0.0.0-20120707110453-a547fc61f48d
8054

8155
replace cloud.google.com/go => cloud.google.com/go v0.49.0

go.sum

Lines changed: 7 additions & 49 deletions
Large diffs are not rendered by default.

pkg/cmds/server/options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636

3737
type ExtraOptions struct {
3838
LicenseFile string
39+
LicenseApiService string
3940
StashImage string
4041
StashImageTag string
4142
DockerRegistry string
@@ -74,6 +75,7 @@ func (s *ExtraOptions) AddFlags(fs *pflag.FlagSet) {
7475
fs.StringVar(&s.DockerRegistry, "docker-registry", s.DockerRegistry, "Docker image registry for sidecar, init-container, check-job, recovery-job and kubectl-job")
7576
fs.StringSliceVar(&s.ImagePullSecrets, "image-pull-secrets", s.ImagePullSecrets, "List of image pull secrets for pulling image from private registries")
7677
fs.StringVar(&s.LicenseFile, "license-file", s.LicenseFile, "Path to license file")
78+
fs.StringVar(&s.LicenseApiService, "license-apiservice", s.LicenseApiService, "Name of the ApiService to use by the addons to identify the respective service and certificate for license verification request")
7779

7880
fs.Float64Var(&s.QPS, "qps", s.QPS, "The maximum QPS to the master from this client")
7981
fs.IntVar(&s.Burst, "burst", s.Burst, "The maximum burst for throttle")
@@ -90,6 +92,8 @@ func (s *ExtraOptions) AddFlags(fs *pflag.FlagSet) {
9092
func (s *ExtraOptions) ApplyTo(cfg *controller.Config) error {
9193
var err error
9294

95+
cfg.LicenseFile = s.LicenseFile
96+
cfg.LicenseApiService = s.LicenseApiService
9397
cfg.StashImage = s.StashImage
9498
cfg.StashImageTag = s.StashImageTag
9599
cfg.DockerRegistry = s.DockerRegistry

pkg/controller/backup_session.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ func (c *StashController) ensureBackupJob(invoker apis.Invoker, targetInfo apis.
287287
if err != nil {
288288
return err
289289
}
290+
// Give the ServiceAccount permission to send request to the license handler
291+
err = stash_rbac.EnsureLicenseReaderClusterRoleBinding(c.kubeClient, invoker.OwnerRef, invoker.ObjectMeta.Namespace, serviceAccountName, invoker.Labels)
292+
if err != nil {
293+
return err
294+
}
290295

291296
// if the Stash is using a private registry, then ensure the image pull secrets
292297
var imagePullSecrets []core.LocalObjectReference
@@ -327,6 +332,8 @@ func (c *StashController) ensureBackupJob(invoker apis.Invoker, targetInfo apis.
327332
implicitInputs[apis.StashDockerRegistry] = c.DockerRegistry
328333
implicitInputs[apis.StashDockerImage] = c.StashImage
329334
implicitInputs[apis.StashImageTag] = c.StashImageTag
335+
// license related inputs
336+
implicitInputs[apis.LicenseApiService] = c.LicenseApiService
330337

331338
taskResolver := resolve.TaskResolver{
332339
StashClient: c.stashClient,

pkg/controller/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const (
4444
)
4545

4646
type config struct {
47+
LicenseFile string
48+
LicenseApiService string
4749
StashImage string
4850
StashImageTag string
4951
DockerRegistry string

pkg/controller/restore_session.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,11 @@ func (c *StashController) ensureRestoreJob(invoker apis.RestoreInvoker, index in
486486
if err != nil {
487487
return err
488488
}
489+
// Give the ServiceAccount permission to send request to the license handler
490+
err = stash_rbac.EnsureLicenseReaderClusterRoleBinding(c.kubeClient, invoker.OwnerRef, invoker.ObjectMeta.Namespace, serviceAccountName, invoker.Labels)
491+
if err != nil {
492+
return err
493+
}
489494

490495
// if the Stash is using a private registry, then ensure the image pull secrets
491496
var imagePullSecrets []core.LocalObjectReference
@@ -662,6 +667,8 @@ func (c *StashController) resolveRestoreTask(invoker apis.RestoreInvoker, reposi
662667
implicitInputs[apis.StashDockerRegistry] = c.DockerRegistry
663668
implicitInputs[apis.StashDockerImage] = c.StashImage
664669
implicitInputs[apis.StashImageTag] = c.StashImageTag
670+
// license related inputs
671+
implicitInputs[apis.LicenseApiService] = c.LicenseApiService
665672

666673
taskResolver := resolve.TaskResolver{
667674
StashClient: c.stashClient,

pkg/rbac/jobs.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,28 @@ func EnsureRepoReaderRolebindingDeleted(kubeClient kubernetes.Interface, stashCl
290290
glog.Infof("Deleted repo-reader rolebinding: " + GetRepoReaderRoleBindingName(meta.Name, meta.Namespace))
291291
return nil
292292
}
293+
294+
func EnsureLicenseReaderClusterRoleBinding(kc kubernetes.Interface, owner *metav1.OwnerReference, namespace, sa string, labels map[string]string) error {
295+
meta := metav1.ObjectMeta{
296+
Name: meta_util.NameWithSuffix(apis.LicenseReader, sa),
297+
Labels: labels,
298+
}
299+
_, _, err := rbac_util.CreateOrPatchClusterRoleBinding(context.TODO(), kc, meta, func(in *rbac.ClusterRoleBinding) *rbac.ClusterRoleBinding {
300+
core_util.EnsureOwnerReference(&in.ObjectMeta, owner)
301+
302+
in.RoleRef = rbac.RoleRef{
303+
APIGroup: rbac.GroupName,
304+
Kind: apis.KindClusterRole,
305+
Name: apis.LicenseReader,
306+
}
307+
in.Subjects = []rbac.Subject{
308+
{
309+
Kind: rbac.ServiceAccountKind,
310+
Name: sa,
311+
Namespace: namespace,
312+
},
313+
}
314+
return in
315+
}, metav1.PatchOptions{})
316+
return err
317+
}

pkg/server/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"stash.appscode.dev/stash/pkg/eventer"
3131
snapregistry "stash.appscode.dev/stash/pkg/registry/snapshot"
3232

33+
license "go.bytebuilders.dev/license-verifier/kubernetes"
3334
admission "k8s.io/api/admission/v1beta1"
3435
core "k8s.io/api/core/v1"
3536
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -127,6 +128,9 @@ func (c completedConfig) New() (*StashServer, error) {
127128
if err != nil {
128129
return nil, err
129130
}
131+
132+
license.NewLicenseEnforcer(c.ExtraConfig.ClientConfig, c.ExtraConfig.LicenseFile).Install(genericServer.Handler.NonGoRestfulMux)
133+
130134
ctrl, err := c.ExtraConfig.New()
131135
if err != nil {
132136
return nil, err
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
6+
# Folders
7+
_obj
8+
_test
9+
10+
# Architecture specific extensions/prefixes
11+
*.cgo1.go
12+
*.cgo2.c
13+
_cgo_defun.c
14+
_cgo_gotypes.go
15+
_cgo_export.*
16+
17+
_testmain.go
18+
19+
*.exe
20+
*.test
21+
*.prof
22+
23+
/bin
24+
/.go
25+
26+
/.idea
27+
/.markdownlint.json
28+
/.vscode
29+
/apiserver.local.config
30+
/coverage.txt
31+
/dist
32+
/hack/config/.env
33+
/test/e2e/junit.xml
34+
/test/e2e/report.xml
35+
36+
.terraform
37+
*.tfstate*

0 commit comments

Comments
 (0)