'Kubectl create multiline secret

I'm trying to put a Service Account into a secret - I did it previously a year ago and it works but now - no matter how I approach it, the application doesn't see it right and says there is Input byte array has incorrect ending byte - When creating normal secret I know you've gotta do it with a new line so

echo -n "secret" | base64

and put that value in secret and apply, but my multiline file

cat secret.json
{
  "type": "service_account",
  "project_id": "smth-smth",
  "private_key_id": "blabla"
...
}

No matter how I approach - whether put it by hand like in the first example, or do it with

cat secret.json | base64

# or 

base64 < secret.json

the secret is created but application throws

Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 3104

When I compare the new secret to the last one of the service account the difference is how the output looks like

The working one is smth like this - when I try to decrypt the base64

echo -n "<long string of base64 encrypred sa> | base64 -D
    { "type": "service_account", "project_id": "blabla"... }

so it's in one line, and the new SA I try to decrypt is outputed in the format as in the file - so each part of json in new line - I tried manually putting it all in one line but without success

Anyone know ? how to put a multiline file in a secret (base64) properly?



Solution 1:[1]

The easiest way to create a secret from a file is to use kubectl create secret generic.

Put your file secret.json in a folder config and then run:

kubectl create secret generic my-secret --from-file=config

You will get a secret my-secret with one key secret.json containing your file (which you can then mount to a pod volume).

Solution 2:[2]

If you cannot create files an option is to write into a variable and then load the result into a --file-literal. This may be necessary because it seems kubectl either escapes newline characters \n when inside a quoted string and ignores them if no quotes are supplied. When reading from a variable the \nare treated as expected.

EDIT: With regards to multi-line strings do take care to use correct linefeed characters, as explained here. I ran into that when trying my answer at home :)

target_string=$(echo "string1\nstring2")
kubectl create secret generic your-secret-name --from-literal=your_key=$target_string

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 derkoe
Solution 2