-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5445ff4
commit ffd6954
Showing
12 changed files
with
283 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright Authors of K9s | ||
|
||
package dao | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// Secret represents a secret K8s resource. | ||
type Secret struct { | ||
Table | ||
decode bool | ||
} | ||
|
||
// Describe describes a secret that can be encoded or decoded. | ||
func (s *Secret) Describe(path string) (string, error) { | ||
encodedDescription, err := s.Table.Describe(path) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if !s.decode { | ||
return encodedDescription, nil | ||
} | ||
|
||
return s.Decode(encodedDescription, path) | ||
} | ||
|
||
// SetDecode sets the decode flag. | ||
func (s *Secret) SetDecode(flag bool) { | ||
s.decode = flag | ||
} | ||
|
||
// Decode removes the ecnode part from the secret's description and appends the | ||
// secret's decoded data. | ||
func (s *Secret) Decode(encodedDescription, path string) (string, error) { | ||
o, err := s.GetFactory().Get(s.GVR(), path, true, labels.Everything()) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
dataEndIndex := strings.Index(encodedDescription, "====") | ||
|
||
if dataEndIndex == -1 { | ||
return "", fmt.Errorf("Unable to find data section in secret description") | ||
} | ||
|
||
dataEndIndex += 4 | ||
|
||
if dataEndIndex >= len(encodedDescription) { | ||
return "", fmt.Errorf("Data section in secret description is invalid") | ||
} | ||
|
||
// Remove the encoded part from k8s's describe API | ||
// More details about the reasoning of index: https://github.com/kubernetes/kubectl/blob/v0.29.0/pkg/describe/describe.go#L2542 | ||
body := encodedDescription[0:dataEndIndex] | ||
|
||
d, err := ExtractSecrets(o.(*unstructured.Unstructured)) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
decodedSecrets := "" | ||
|
||
for k, v := range d { | ||
decodedSecrets = fmt.Sprintf("%s\n%s:\t%s", decodedSecrets, k, v) | ||
} | ||
|
||
return fmt.Sprintf("%s%s", body, decodedSecrets), nil | ||
} | ||
|
||
// ExtractSecrets takes an unstructured object and attempts to convert it into a | ||
// Kubernetes Secret. | ||
// It returns a map where the keys are the secret data keys and the values are | ||
// the corresponding secret data values. | ||
// If the conversion fails, it returns an error. | ||
func ExtractSecrets(o *unstructured.Unstructured) (map[string]string, error) { | ||
var secret v1.Secret | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(o.Object, &secret) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
secretData := make(map[string]string, len(secret.Data)) | ||
|
||
for k, val := range secret.Data { | ||
secretData[k] = string(val) | ||
} | ||
|
||
return secretData, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of K9s | ||
|
||
package dao_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/derailed/k9s/internal/client" | ||
"github.com/derailed/k9s/internal/dao" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEncodedSecretDescribe(t *testing.T) { | ||
s := dao.Secret{} | ||
s.Init(makeFactory(), client.NewGVR("v1/secrets")) | ||
|
||
encodedString := | ||
` | ||
Name: bootstrap-token-abcdef | ||
Namespace: kube-system | ||
Labels: <none> | ||
Annotations: <none> | ||
Type: generic | ||
Data | ||
==== | ||
token-secret: 24 bytes` | ||
|
||
expected := "\nName: bootstrap-token-abcdef\n" + | ||
"Namespace: kube-system\n" + | ||
"Labels: <none>\n" + | ||
"Annotations: <none>\n" + | ||
"\n" + | ||
"Type: generic\n" + | ||
"\n" + | ||
"Data\n" + | ||
"====\n" + | ||
"token-secret:\t0123456789abcdef" | ||
|
||
decodedDescription, _ := s.Decode(encodedString, "kube-system/bootstrap-token-abcdef") | ||
assert.Equal(t, expected, decodedDescription) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"apiVersion": "v1", | ||
"data": { | ||
"token-secret": "MDEyMzQ1Njc4OWFiY2RlZg==" | ||
}, | ||
"kind": "Secret", | ||
"metadata": { | ||
"creationTimestamp": "2024-01-15T18:19:00Z", | ||
"name": "bootstrap-token-abcdef", | ||
"namespace": "kube-system", | ||
"resourceVersion": "243", | ||
"uid": "6f5695d4-c0f4-4b65-890a-b1115ffd1f3b" | ||
}, | ||
"type": "bootstrap.kubernetes.io/token" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package dao_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/derailed/k9s/internal/client" | ||
"github.com/derailed/k9s/internal/dao" | ||
"github.com/derailed/k9s/internal/watch" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/informers" | ||
) | ||
|
||
type testFactory struct { | ||
inventory map[string]map[string][]runtime.Object | ||
} | ||
|
||
func makeFactory() dao.Factory { | ||
return &testFactory{ | ||
inventory: map[string]map[string][]runtime.Object{ | ||
"kube-system": { | ||
"v1/secrets": { | ||
load("secret"), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
var _ dao.Factory = &testFactory{} | ||
|
||
func (f *testFactory) Client() client.Connection { | ||
return nil | ||
} | ||
func (f *testFactory) Get(gvr, fqn string, wait bool, sel labels.Selector) (runtime.Object, error) { | ||
ns, po := path.Split(fqn) | ||
ns = strings.Trim(ns, "/") | ||
|
||
for _, o := range f.inventory[ns][gvr] { | ||
if o.(*unstructured.Unstructured).GetName() == po { | ||
return o, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
func (f *testFactory) List(gvr, ns string, wait bool, sel labels.Selector) ([]runtime.Object, error) { | ||
return f.inventory[ns][gvr], nil | ||
} | ||
|
||
func (f *testFactory) ForResource(ns, gvr string) (informers.GenericInformer, error) { | ||
return nil, nil | ||
} | ||
func (f *testFactory) CanForResource(ns, gvr string, verbs []string) (informers.GenericInformer, error) { | ||
return nil, nil | ||
} | ||
func (f *testFactory) WaitForCacheSync() {} | ||
func (f *testFactory) Forwarders() watch.Forwarders { | ||
return nil | ||
} | ||
func (f *testFactory) DeleteForwarder(string) {} | ||
|
||
type testResource struct{} | ||
|
||
func load(n string) *unstructured.Unstructured { | ||
raw, _ := os.ReadFile(fmt.Sprintf("testdata/%s.json", n)) | ||
|
||
var o unstructured.Unstructured | ||
json.Unmarshal(raw, &o) | ||
|
||
return &o | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.