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

Implements handling of insecure TLS connections in Mehkit #373

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c75d194
Implements handling of insecure TLS connections in Mehkit
yash37158 Sep 20, 2023
27ac06d
Merge branch 'master' into master
yash37158 Sep 25, 2023
3ea693d
Fixing go formating changes
yash37158 Sep 26, 2023
f0a66b1
Merge remote-tracking branch 'origin'
yash37158 Sep 26, 2023
2a49b89
Merge branch 'meshery:master' into master
yash37158 Sep 29, 2023
0380716
Fixing formatting errors
yash37158 Sep 29, 2023
3b8ed1c
Merge branch 'meshery:master' into master
yash37158 Oct 2, 2023
48fbcbf
fix: Fix format script
yash37158 Oct 2, 2023
3d84b71
Fixing nil pointer dereferences
yash37158 Oct 2, 2023
3b9eee7
Merge branch 'meshery:master' into master
yash37158 Oct 10, 2023
7ecfe12
Adding CertificateAuthorityData capabilities when flag is true
yash37158 Oct 10, 2023
50af4d7
Merge remote-tracking branch 'origin/master'
yash37158 Oct 10, 2023
a4485cf
fixing suggestion from muzair on adding events
yash37158 Oct 24, 2023
2ee76b0
Merge branch 'master' into master
yash37158 Oct 24, 2023
848bee4
Update utils/kubernetes/client.go
yash37158 Oct 26, 2023
9a613e7
Update utils/kubernetes/client.go
yash37158 Oct 26, 2023
d4522c1
Fixing indentation utils/kubernetes/client.go
yash37158 Oct 26, 2023
ba210fe
Fixing DCO changes
yash37158 Oct 27, 2023
3137d5b
Merge branch 'master' into master
yash37158 Oct 30, 2023
5427339
Accepting changes from Aisuko
yash37158 Nov 1, 2023
8827e75
Merge branch 'master' into master
yash37158 Nov 1, 2023
5c687da
Merge branch 'master' into master
yash37158 Nov 10, 2023
899223a
Removing logging of komposeObject
yash37158 Nov 10, 2023
634d994
Merge branch 'master' into master
yash37158 Nov 13, 2023
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
210 changes: 151 additions & 59 deletions utils/kubernetes/client.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,162 @@
package kubernetes

import (
"os"
"path/filepath"
"os"

Check failure on line 4 in utils/kubernetes/client.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
"path/filepath"

"github.com/layer5io/meshkit/models"

Check failure on line 7 in utils/kubernetes/client.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/golangci/golangci-lint (goimports)
"gopkg.in/yaml.v2"
"github.com/layer5io/meshkit/utils"
events "github.com/layer5io/meshkit/utils/events"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"log"

"github.com/layer5io/meshkit/utils"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

// CustomEvent is a custom event type for recording events.
type CustomEvent struct {
yash37158 marked this conversation as resolved.
Show resolved Hide resolved
EventType string
Level string
Message string
}

// DetectKubeConfig detects the kubeconfig for the kubernetes cluster and returns it
func DetectKubeConfig(configfile []byte) (config *rest.Config, err error) {
if len(configfile) > 0 {
cfgFile, err := processConfig(configfile)
if err != nil {
return nil, err
}

if config, err = clientcmd.RESTConfigFromKubeConfig(cfgFile); err == nil {
return config, err
}
}

// If deployed within the cluster
if config, err = rest.InClusterConfig(); err == nil {
return config, err
}

// Look for kubeconfig from the path mentioned in $KUBECONFIG
kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig != "" {
if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err == nil {
return config, err
}
}

// Look for kubeconfig at the default path
path := filepath.Join(utils.GetHome(), ".kube", "config")
if config, err = clientcmd.BuildConfigFromFlags("", path); err == nil {
return config, err
}

return
if len(configfile) > 0 {
yash37158 marked this conversation as resolved.
Show resolved Hide resolved
cfgFile, err := processConfig(configfile)
if err != nil {
return nil, err
}

models := &models.Kubeconfig{}

cfgFile, err = processConfig(configfile)
if err != nil {
return nil, err
}

if err := yaml.Unmarshal(cfgFile, models); err != nil {
return nil, err
}

for _, clusters := range models.Clusters {
if config, err = clientcmd.RESTConfigFromKubeConfig(cfgFile); err == nil {
// Check the `InsecureSkipTLSVerify` field
if insecureSkipTLSVerify := clusters.Cluster.InsecureSkipTLSVerify; insecureSkipTLSVerify != nil && *insecureSkipTLSVerify {
// Skip TLS verification if the field is set to true
config.TLSClientConfig.Insecure = *insecureSkipTLSVerify

// Create an event to record the insecure connection
event := CustomEvent{
EventType: "Warning",
Level: "Warning",
Message: "Insecure connection to Kubernetes cluster detected",
}

log.Println("SKIP TLS verification part was called")


// Send the event to the client
handleCustomEvent(event)

Check failure on line 63 in utils/kubernetes/client.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}

return config, err
}
}
}

// If deployed within the cluster
if config, err = rest.InClusterConfig(); err == nil {
return config, err
}

// Look for kubeconfig from the path mentioned in $KUBECONFIGZ
var models models.Kubeconfig
kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig != "" {
if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err == nil {
for _, cluster := range models.Clusters {
if cluster.Cluster.InsecureSkipTLSVerify != nil {
// Skip TLS verification for this cluster
config.TLSClientConfig.Insecure = true

// Create an event to record the insecure connection
event := CustomEvent{
EventType: "Warning",
Level: "Warning",
Message: "Insecure connection to Kubernetes cluster detected",
}
log.Println("SKIP TLS verification part was called")

handleCustomEvent(event)
}
}

return config, err
}
}

// Look for kubeconfig at the default path
path := filepath.Join(utils.GetHome(), ".kube", "config")
if config, err = clientcmd.BuildConfigFromFlags("", path); err == nil {
for _, cluster := range models.Clusters {
if cluster.Cluster.InsecureSkipTLSVerify != nil {
// Skip TLS verification for this cluster
config.TLSClientConfig.Insecure = true

// Create an event to record the insecure connection
event := CustomEvent{
EventType: "Warning",
Level: "Warning",
Message: "Insecure connection to Kubernetes cluster detected",
}
log.Println("SKIP TLS verification part was called")

handleCustomEvent(event)
}
}
return config, err
}

return
}

func processConfig(configFile []byte) ([]byte, error) {
cfg, err := clientcmd.Load(configFile)
if err != nil {
return nil, ErrLoadConfig(err)
}

err = clientcmdapi.MinifyConfig(cfg)
if err != nil {
return nil, ErrValidateConfig(err)
}

err = clientcmdapi.FlattenConfig(cfg)
if err != nil {
return nil, ErrValidateConfig(err)
}

err = clientcmd.Validate(*cfg)
if err != nil {
return nil, ErrValidateConfig(err)
}

return clientcmd.Write(*cfg)
func handleCustomEvent(event CustomEvent) {

Check failure on line 127 in utils/kubernetes/client.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)
yash37158 marked this conversation as resolved.
Show resolved Hide resolved

eventStreamer := events.NewEventStreamer()

clientChannel := make(chan interface{})
eventStreamer.Subscribe(clientChannel)
eventStreamer.Publish(event)

// log.Printf("%s: %s", event.EventType, event.Message)

Check failure on line 136 in utils/kubernetes/client.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}


func processConfig(configFile []byte) ([]byte, error) {
cfg, err := clientcmd.Load(configFile)
if err != nil {
return nil, ErrLoadConfig(err)
}

err = clientcmdapi.MinifyConfig(cfg)
if err != nil {
return nil, ErrValidateConfig(err)
}

err = clientcmdapi.FlattenConfig(cfg)
if err != nil {
return nil, ErrValidateConfig(err)
}

err = clientcmd.Validate(*cfg)
if err != nil {
return nil, ErrValidateConfig(err)
}

return clientcmd.Write(*cfg)
}
126 changes: 126 additions & 0 deletions utils/kubernetes/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package kubernetes

import (
"fmt"

Check failure on line 4 in utils/kubernetes/client_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
"testing"
)

func TestDetectKubeConfig(t *testing.T) {
yash37158 marked this conversation as resolved.
Show resolved Hide resolved
// Test case 1: Insecure TLS verification for context1
testKubeConfig1 := []byte(`
apiVersion: v1
clusters:
- name: cluster1
cluster:
server: https://localhost:9443
insecure-skip-tls-verify: true
contexts:
- context:
cluster: cluster1
user: user1
name: context1
current-context: context1
kind: Config
preferences: {}
users:
- name: user1
user:
`)
config1, err1 := DetectKubeConfig(testKubeConfig1)
if err1 != nil {
t.Errorf("Test case 1: Error while detecting kubeconfig: %v", err1)
}
if config1 == nil {

Check failure on line 33 in utils/kubernetes/client_test.go

View workflow job for this annotation

GitHub Actions / lint

SA5011(related information): this check suggests that the pointer can be nil (staticcheck)
t.Errorf("Test case 1: Config should not be nil")
}
if !config1.TLSClientConfig.Insecure {

Check failure on line 36 in utils/kubernetes/client_test.go

View workflow job for this annotation

GitHub Actions / lint

SA5011: possible nil pointer dereference (staticcheck)
t.Errorf("Test case 1: TLS verification should be skipped, but it's not")
}

// Test case 2: Secure TLS verification for context2
testKubeConfig2 := []byte(`
apiVersion: v1
clusters:
- name: cluster2
cluster:
server: https://localhost:9443
insecure-skip-tls-verify: false
contexts:
- context:
cluster: cluster2
user: user2
name: context2
current-context: context2
kind: Config
preferences: {}
users:
- name: user2
user:
`)
config2, err2 := DetectKubeConfig(testKubeConfig2)
if err2 != nil {
t.Errorf("Test case 2: Error while detecting kubeconfig: %v", err2)
}
if config2 == nil {
t.Errorf("Test case 2: Config should not be nil")
}
if config2.TLSClientConfig.Insecure {
t.Errorf("Test case 2: TLS verification should not be skipped, but it is")
}

// Test case 3: Multi-context kubeconfig with mixed TLS settings
testKubeConfig3 := []byte(`
apiVersion: v1
clusters:
- name: cluster1
cluster:
server: https://localhost:9443
insecure-skip-tls-verify: true
- name: cluster2
cluster:
server: https://localhost:9443
insecure-skip-tls-verify: false
contexts:
- context:
cluster: cluster1
user: user1
name: context1
- context:
cluster: cluster2
user: user2
name: context2
current-context: context1
kind: Config
preferences: {}
users:
- name: user1
user:
- name: user2
user:
`)
config3, err3 := DetectKubeConfig(testKubeConfig3)
if err3 != nil {
t.Errorf("Test case 3: Error while detecting kubeconfig: %v", err3)
}
if config3 == nil {
t.Errorf("Test case 3: Config should not be nil")
}
if !config3.TLSClientConfig.Insecure {
t.Errorf("Test case 3: TLS verification should be skipped, but it's not")
}

event := CustomEvent{
EventType: "Warning",
Level: "Warning",
Message: "Insecure connection to Kubernetes cluster detected",
}

handleCustomEvent(event)


// Print whether TLS verification is skipped or not for each test case
fmt.Printf("Test case 1: TLS verification is skipped (Insecure: %v)\n", config1.TLSClientConfig.Insecure)
fmt.Printf("Test case 2: TLS verification is not skipped (Insecure: %v)\n", config2.TLSClientConfig.Insecure)
fmt.Printf("Test case 3: TLS verification is skipped (Insecure: %v)\n", config3.TLSClientConfig.Insecure)

Check failure on line 125 in utils/kubernetes/client_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}
Loading