Repository: derailed/k9s
File: internal/dao/secret.go (Go)

Code before:
}

// 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, "====") + 4

	if dataEndIndex == -1 {
		return "", fmt.Errorf("unable to find data section in secret description")
	}

	d, err := ExtractSecrets(o.(*unstructured.Unstructured))

	if err != nil {
		return "", err
	}

	// 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
	decodedDescription := string([]rune(encodedDescription)[0:dataEndIndex])

	for k, v := range d {
		decodedDescription = fmt.Sprintf("%s\n%s:\t%s", decodedDescription, k, v)
	}

	return decodedDescription, 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)

Review comment (suggestion):
We should check upper bound on the slice here just in case...
Also not need to cast to runes should be able to index directly into str 

Code after:

// Decode removes the encoded 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 := []string{}

	for k, v := range d {
		decodedSecrets = append(decodedSecrets, "\n", k, ":\t", v)
	}

	return body + strings.Join(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 {