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

feat(license): add license feature #702

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
96 changes: 96 additions & 0 deletions src/cluster/k8sClientHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
var parsed bool = false
var kubeconfig *string

type Config struct {
K8sClient *kubernetes.Clientset
}

func isInCluster() bool {
if _, ok := os.LookupEnv("KUBERNETES_PORT"); ok {
return true
Expand Down Expand Up @@ -499,3 +503,95 @@ func GetKubearmorRelayURL() string {
url := "kubearmor." + namespace + ".svc.cluster.local"
return url
}

// GetSecrets to get secrets
func GetSecrets(k8sClient *kubernetes.Clientset, label string, namespace string, name string) (*v1.Secret, error) {
if k8sClient == nil {
return nil, errors.New("k8s client not created")
}

secrets, err := k8sClient.CoreV1().Secrets(namespace).Get(context.Background(), name, metav1.GetOptions{})

if err != nil {
log.Error().Msgf("error while getting license secrets with name: %s from %s namespace, error: %s", name, namespace, err.Error())
return nil, err
}

return secrets, nil

}

// CreateLicenseSecret to create license secret
func CreateLicenseSecret(k8sClient *kubernetes.Clientset, namespace string, key string, userId string, name string, label string) (*v1.Secret, error) {
if k8sClient == nil {
return nil, errors.New("k8s client not created")
}

secret, err := GetSecrets(k8sClient, label, namespace, name)

if err != nil {
log.Error().Msgf("error while fetching secrets, error: %s", err.Error())
return nil, err
}

if secret != nil {
log.Info().Msgf("secrets already exists for discovery-engine license for user-id: %s", userId)
return secret, nil
}
t := true

secretSpec := v1.Secret{

ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
"app": "discovery-engine",
},
},
Immutable: &t,
Data: nil,
StringData: map[string]string{
"key": key,
"user-id": userId,
},
Type: v1.SecretTypeOpaque,
}

secret, err = k8sClient.CoreV1().Secrets(namespace).Create(context.Background(), &secretSpec, metav1.CreateOptions{})
if err != nil {
log.Error().Msgf("error while creating secret for license key, error: %s", err.Error())
return nil, err
}
log.Info().Msgf("secret created successfully for discovery-engine")
return secret, nil
}

// DeleteSecrets to delete secrets
func DeleteSecrets(k8sClient *kubernetes.Clientset, name string, namespace string) error {
if k8sClient == nil {
return errors.New("k8s client not created")
}

err := k8sClient.CoreV1().Secrets(namespace).Delete(context.Background(), name, metav1.DeleteOptions{})
if err != nil {
log.Error().Msgf("error while deleting secrets for discovery-engine, error: %s", err.Error())
return err
}

return nil
}

// GetKubeSystemUUID get kube-system namespace uuid
func GetKubeSystemUUID(k8sClient *kubernetes.Clientset) (string, error) {
if k8sClient == nil {
return "", errors.New("k8s client not created")
}

kubeSystem, err := k8sClient.CoreV1().Namespaces().Get(context.Background(), "kube-system", metav1.GetOptions{})
rajaSahil marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", err
}

return string(kubeSystem.GetUID()), nil

}
2 changes: 2 additions & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ require (
github.com/clarketm/json v1.17.1
github.com/confluentinc/confluent-kafka-go v1.6.1
github.com/go-sql-driver/mysql v1.5.0
github.com/golang-jwt/jwt/v5 v5.0.0-rc.2
github.com/google/go-cmp v0.5.8
github.com/google/go-github v17.0.0+incompatible
github.com/kubearmor/KVMService/src/types v0.0.0-20220714130113-b0eba8c9ff34
github.com/kubearmor/KubeArmor/protobuf v0.0.0-20220504043216-6451e04be58b
github.com/kyverno/kyverno v1.6.10
github.com/mattn/go-sqlite3 v1.14.12
github.com/mervick/aes-everywhere/go/aes256 v0.0.0-20220903070135-f13ed3789ae1
github.com/robfig/cron v1.2.0
github.com/rs/zerolog v1.26.0
github.com/spf13/viper v1.10.1
Expand Down
4 changes: 4 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptG
github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-jwt/jwt/v5 v5.0.0-rc.2 h1:hXPcSazn8wKOfSb9y2m1bdgUMlDxVDarxh3lJVbC6JE=
github.com/golang-jwt/jwt/v5 v5.0.0-rc.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -776,6 +778,8 @@ github.com/mdlayher/netlink v1.3.0/go.mod h1:xK/BssKuwcRXHrtN04UBkwQ6dY9VviGGuri
github.com/mdlayher/netlink v1.4.0/go.mod h1:dRJi5IABcZpBD2A3D0Mv/AiX8I9uDEu5oGkAVrekmf8=
github.com/mdlayher/raw v0.0.0-20190313224157-43dbcdd7739d/go.mod h1:r1fbeITl2xL/zLbVnNHFyOzQJTgr/3fpf1lJX/cjzR8=
github.com/mdlayher/raw v0.0.0-20190606142536-fef19f00fc18/go.mod h1:7EpbotpCmVZcu+KCX4g9WaRNuu11uyhiW7+Le1dKawg=
github.com/mervick/aes-everywhere/go/aes256 v0.0.0-20220903070135-f13ed3789ae1 h1:RLYNaO6dcj6hms2FSzwsXlZsyjxQBJi8qO/8Vkilhz0=
github.com/mervick/aes-everywhere/go/aes256 v0.0.0-20220903070135-f13ed3789ae1/go.mod h1:Eb5RMoo9kOQra/2uRiUTGP+LfNuM13Vqm7y7P34+KKo=
github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721/go.mod h1:Ickgr2WtCLZ2MDGd4Gr0geeCH5HybhRJbonOgQpvSxc=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
Expand Down
Loading