Can Kubernetes secrets store newlines?












2















I've created a secret from a file using a command like:



kubectl create secret generic laravel-oauth 
--from-file=./.work-in-progress/oauth_private.key
--from-file=./.work-in-progress/oauth_public.key


However it seems new lines are stripped from the files (when using the secrets as ENV variables).



There is a 'encoding' note in the docs that state:




The serialized JSON and YAML values of secret data are encoded as
base64 strings. Newlines are not valid within these strings and must
be omitted. When using the base64 utility on Darwin/macOS users should
avoid using the -b option to split long lines. Conversely Linux users
should add the option -w 0 to base64 commands or the pipeline base64 |
tr -d 'n' if -w option is not available.




However I assumed this only applies for 'manually' created secrets via YAML files.










share|improve this question



























    2















    I've created a secret from a file using a command like:



    kubectl create secret generic laravel-oauth 
    --from-file=./.work-in-progress/oauth_private.key
    --from-file=./.work-in-progress/oauth_public.key


    However it seems new lines are stripped from the files (when using the secrets as ENV variables).



    There is a 'encoding' note in the docs that state:




    The serialized JSON and YAML values of secret data are encoded as
    base64 strings. Newlines are not valid within these strings and must
    be omitted. When using the base64 utility on Darwin/macOS users should
    avoid using the -b option to split long lines. Conversely Linux users
    should add the option -w 0 to base64 commands or the pipeline base64 |
    tr -d 'n' if -w option is not available.




    However I assumed this only applies for 'manually' created secrets via YAML files.










    share|improve this question

























      2












      2








      2








      I've created a secret from a file using a command like:



      kubectl create secret generic laravel-oauth 
      --from-file=./.work-in-progress/oauth_private.key
      --from-file=./.work-in-progress/oauth_public.key


      However it seems new lines are stripped from the files (when using the secrets as ENV variables).



      There is a 'encoding' note in the docs that state:




      The serialized JSON and YAML values of secret data are encoded as
      base64 strings. Newlines are not valid within these strings and must
      be omitted. When using the base64 utility on Darwin/macOS users should
      avoid using the -b option to split long lines. Conversely Linux users
      should add the option -w 0 to base64 commands or the pipeline base64 |
      tr -d 'n' if -w option is not available.




      However I assumed this only applies for 'manually' created secrets via YAML files.










      share|improve this question














      I've created a secret from a file using a command like:



      kubectl create secret generic laravel-oauth 
      --from-file=./.work-in-progress/oauth_private.key
      --from-file=./.work-in-progress/oauth_public.key


      However it seems new lines are stripped from the files (when using the secrets as ENV variables).



      There is a 'encoding' note in the docs that state:




      The serialized JSON and YAML values of secret data are encoded as
      base64 strings. Newlines are not valid within these strings and must
      be omitted. When using the base64 utility on Darwin/macOS users should
      avoid using the -b option to split long lines. Conversely Linux users
      should add the option -w 0 to base64 commands or the pipeline base64 |
      tr -d 'n' if -w option is not available.




      However I assumed this only applies for 'manually' created secrets via YAML files.







      kubernetes






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 10:05









      Chris StryczynskiChris Stryczynski

      4,02653064




      4,02653064
























          3 Answers
          3






          active

          oldest

          votes


















          1














          The new lines are not stripped the files are just base64 encoded as mentioned in the other answers too. For example:



          # mycert.pem
          -----BEGIN CERTIFICATE-----
          xxxxxx
          xxxxxx
          ...
          -----END CERTIFICATE-----


          Then:



          $ kubectl create secret generic mysecret --from-file=./cert.pem


          Then:



          $ kubectl get secret mysecret -o=yaml

          apiVersion: v1
          data:
          cert.pem: <base64 encoded string>
          kind: Secret
          metadata:
          creationTimestamp: 2018-11-14T18:11:46Z
          name: mysecret
          namespace: default
          resourceVersion: "20180431"
          selfLink: /api/v1/namespaces/default/secrets/mysecret
          uid: xxxxxx
          type: Opaque


          Then if you decode it, you will get the original secret.



          $ echo '<base64 encoded string>' | base64 -D
          -----BEGIN CERTIFICATE-----
          xxxxxx
          xxxxxx
          ...
          -----END CERTIFICATE-----


          Also, this is not necessarily secure at rest. If you are looking for more security you can use something like Hashicorp Vault or as alluded by @Alex Bitnami's sealed secrets.






          share|improve this answer
























          • This did not seem to be the case when I mounted the secret as a volume.

            – Chris Stryczynski
            Nov 14 '18 at 18:50











          • What does it look like when you mount it? base64? non base64 with no newlines?

            – Rico
            Nov 14 '18 at 18:52



















          2














          The note you refer to is for the base64 encoded string itself (not the content that was encoded).



          Using secrets as env var will potentially expose them via the dashboard "preview eye" (if you use the Kube Dashboard), you should mount them into a directory and make the app load them from there instead; I fell for that too and was surprised I was able to view the secret.



          I've not come across the stripping of new line characters, as the above command would simply do a base64 of the content (including new line chars). That said, storing the secrets b64 encoded is not exactly safe either, you should consider using sealed-secrets (bitnami) instead, it works just like normal secrets, but is actually encrypted at rest.



          HTH,
          Alex






          share|improve this answer































            0














            It seems newlines work fine (maybe I ran into another issue earlier).



            Here is a full example:



            #!/usr/bin/env bash
            set -euo pipefail

            printf "123n456n789" > ./.work-in-progress/example.txt

            kubectl create secret generic example-test
            --from-file=./.work-in-progress/example.txt
            --dry-run -o yaml | kubectl apply -f -

            cat <<EOF | kubectl apply -f -
            apiVersion: extensions/v1beta1
            kind: Deployment
            metadata:
            name: example
            labels:
            app: example
            spec:
            replicas: 1
            selector:
            matchLabels:
            app: example
            template:
            metadata:
            labels:
            app: example
            spec:
            volumes:
            - name: example-test-volume
            secret:
            secretName: example-test
            containers:
            - name: app
            command: ["sleep", "99999999"]
            image: busybox:latest
            imagePullPolicy: IfNotPresent
            volumeMounts:
            - name: example-test-volume
            mountPath: /tmp/example
            env:
            - name: exampleenv
            valueFrom:
            secretKeyRef:
            name: example-test
            key: example.txt


            EOF





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297572%2fcan-kubernetes-secrets-store-newlines%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              The new lines are not stripped the files are just base64 encoded as mentioned in the other answers too. For example:



              # mycert.pem
              -----BEGIN CERTIFICATE-----
              xxxxxx
              xxxxxx
              ...
              -----END CERTIFICATE-----


              Then:



              $ kubectl create secret generic mysecret --from-file=./cert.pem


              Then:



              $ kubectl get secret mysecret -o=yaml

              apiVersion: v1
              data:
              cert.pem: <base64 encoded string>
              kind: Secret
              metadata:
              creationTimestamp: 2018-11-14T18:11:46Z
              name: mysecret
              namespace: default
              resourceVersion: "20180431"
              selfLink: /api/v1/namespaces/default/secrets/mysecret
              uid: xxxxxx
              type: Opaque


              Then if you decode it, you will get the original secret.



              $ echo '<base64 encoded string>' | base64 -D
              -----BEGIN CERTIFICATE-----
              xxxxxx
              xxxxxx
              ...
              -----END CERTIFICATE-----


              Also, this is not necessarily secure at rest. If you are looking for more security you can use something like Hashicorp Vault or as alluded by @Alex Bitnami's sealed secrets.






              share|improve this answer
























              • This did not seem to be the case when I mounted the secret as a volume.

                – Chris Stryczynski
                Nov 14 '18 at 18:50











              • What does it look like when you mount it? base64? non base64 with no newlines?

                – Rico
                Nov 14 '18 at 18:52
















              1














              The new lines are not stripped the files are just base64 encoded as mentioned in the other answers too. For example:



              # mycert.pem
              -----BEGIN CERTIFICATE-----
              xxxxxx
              xxxxxx
              ...
              -----END CERTIFICATE-----


              Then:



              $ kubectl create secret generic mysecret --from-file=./cert.pem


              Then:



              $ kubectl get secret mysecret -o=yaml

              apiVersion: v1
              data:
              cert.pem: <base64 encoded string>
              kind: Secret
              metadata:
              creationTimestamp: 2018-11-14T18:11:46Z
              name: mysecret
              namespace: default
              resourceVersion: "20180431"
              selfLink: /api/v1/namespaces/default/secrets/mysecret
              uid: xxxxxx
              type: Opaque


              Then if you decode it, you will get the original secret.



              $ echo '<base64 encoded string>' | base64 -D
              -----BEGIN CERTIFICATE-----
              xxxxxx
              xxxxxx
              ...
              -----END CERTIFICATE-----


              Also, this is not necessarily secure at rest. If you are looking for more security you can use something like Hashicorp Vault or as alluded by @Alex Bitnami's sealed secrets.






              share|improve this answer
























              • This did not seem to be the case when I mounted the secret as a volume.

                – Chris Stryczynski
                Nov 14 '18 at 18:50











              • What does it look like when you mount it? base64? non base64 with no newlines?

                – Rico
                Nov 14 '18 at 18:52














              1












              1








              1







              The new lines are not stripped the files are just base64 encoded as mentioned in the other answers too. For example:



              # mycert.pem
              -----BEGIN CERTIFICATE-----
              xxxxxx
              xxxxxx
              ...
              -----END CERTIFICATE-----


              Then:



              $ kubectl create secret generic mysecret --from-file=./cert.pem


              Then:



              $ kubectl get secret mysecret -o=yaml

              apiVersion: v1
              data:
              cert.pem: <base64 encoded string>
              kind: Secret
              metadata:
              creationTimestamp: 2018-11-14T18:11:46Z
              name: mysecret
              namespace: default
              resourceVersion: "20180431"
              selfLink: /api/v1/namespaces/default/secrets/mysecret
              uid: xxxxxx
              type: Opaque


              Then if you decode it, you will get the original secret.



              $ echo '<base64 encoded string>' | base64 -D
              -----BEGIN CERTIFICATE-----
              xxxxxx
              xxxxxx
              ...
              -----END CERTIFICATE-----


              Also, this is not necessarily secure at rest. If you are looking for more security you can use something like Hashicorp Vault or as alluded by @Alex Bitnami's sealed secrets.






              share|improve this answer













              The new lines are not stripped the files are just base64 encoded as mentioned in the other answers too. For example:



              # mycert.pem
              -----BEGIN CERTIFICATE-----
              xxxxxx
              xxxxxx
              ...
              -----END CERTIFICATE-----


              Then:



              $ kubectl create secret generic mysecret --from-file=./cert.pem


              Then:



              $ kubectl get secret mysecret -o=yaml

              apiVersion: v1
              data:
              cert.pem: <base64 encoded string>
              kind: Secret
              metadata:
              creationTimestamp: 2018-11-14T18:11:46Z
              name: mysecret
              namespace: default
              resourceVersion: "20180431"
              selfLink: /api/v1/namespaces/default/secrets/mysecret
              uid: xxxxxx
              type: Opaque


              Then if you decode it, you will get the original secret.



              $ echo '<base64 encoded string>' | base64 -D
              -----BEGIN CERTIFICATE-----
              xxxxxx
              xxxxxx
              ...
              -----END CERTIFICATE-----


              Also, this is not necessarily secure at rest. If you are looking for more security you can use something like Hashicorp Vault or as alluded by @Alex Bitnami's sealed secrets.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 14 '18 at 18:23









              RicoRico

              27.7k94966




              27.7k94966













              • This did not seem to be the case when I mounted the secret as a volume.

                – Chris Stryczynski
                Nov 14 '18 at 18:50











              • What does it look like when you mount it? base64? non base64 with no newlines?

                – Rico
                Nov 14 '18 at 18:52



















              • This did not seem to be the case when I mounted the secret as a volume.

                – Chris Stryczynski
                Nov 14 '18 at 18:50











              • What does it look like when you mount it? base64? non base64 with no newlines?

                – Rico
                Nov 14 '18 at 18:52

















              This did not seem to be the case when I mounted the secret as a volume.

              – Chris Stryczynski
              Nov 14 '18 at 18:50





              This did not seem to be the case when I mounted the secret as a volume.

              – Chris Stryczynski
              Nov 14 '18 at 18:50













              What does it look like when you mount it? base64? non base64 with no newlines?

              – Rico
              Nov 14 '18 at 18:52





              What does it look like when you mount it? base64? non base64 with no newlines?

              – Rico
              Nov 14 '18 at 18:52













              2














              The note you refer to is for the base64 encoded string itself (not the content that was encoded).



              Using secrets as env var will potentially expose them via the dashboard "preview eye" (if you use the Kube Dashboard), you should mount them into a directory and make the app load them from there instead; I fell for that too and was surprised I was able to view the secret.



              I've not come across the stripping of new line characters, as the above command would simply do a base64 of the content (including new line chars). That said, storing the secrets b64 encoded is not exactly safe either, you should consider using sealed-secrets (bitnami) instead, it works just like normal secrets, but is actually encrypted at rest.



              HTH,
              Alex






              share|improve this answer




























                2














                The note you refer to is for the base64 encoded string itself (not the content that was encoded).



                Using secrets as env var will potentially expose them via the dashboard "preview eye" (if you use the Kube Dashboard), you should mount them into a directory and make the app load them from there instead; I fell for that too and was surprised I was able to view the secret.



                I've not come across the stripping of new line characters, as the above command would simply do a base64 of the content (including new line chars). That said, storing the secrets b64 encoded is not exactly safe either, you should consider using sealed-secrets (bitnami) instead, it works just like normal secrets, but is actually encrypted at rest.



                HTH,
                Alex






                share|improve this answer


























                  2












                  2








                  2







                  The note you refer to is for the base64 encoded string itself (not the content that was encoded).



                  Using secrets as env var will potentially expose them via the dashboard "preview eye" (if you use the Kube Dashboard), you should mount them into a directory and make the app load them from there instead; I fell for that too and was surprised I was able to view the secret.



                  I've not come across the stripping of new line characters, as the above command would simply do a base64 of the content (including new line chars). That said, storing the secrets b64 encoded is not exactly safe either, you should consider using sealed-secrets (bitnami) instead, it works just like normal secrets, but is actually encrypted at rest.



                  HTH,
                  Alex






                  share|improve this answer













                  The note you refer to is for the base64 encoded string itself (not the content that was encoded).



                  Using secrets as env var will potentially expose them via the dashboard "preview eye" (if you use the Kube Dashboard), you should mount them into a directory and make the app load them from there instead; I fell for that too and was surprised I was able to view the secret.



                  I've not come across the stripping of new line characters, as the above command would simply do a base64 of the content (including new line chars). That said, storing the secrets b64 encoded is not exactly safe either, you should consider using sealed-secrets (bitnami) instead, it works just like normal secrets, but is actually encrypted at rest.



                  HTH,
                  Alex







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 13:33









                  AlexAlex

                  211




                  211























                      0














                      It seems newlines work fine (maybe I ran into another issue earlier).



                      Here is a full example:



                      #!/usr/bin/env bash
                      set -euo pipefail

                      printf "123n456n789" > ./.work-in-progress/example.txt

                      kubectl create secret generic example-test
                      --from-file=./.work-in-progress/example.txt
                      --dry-run -o yaml | kubectl apply -f -

                      cat <<EOF | kubectl apply -f -
                      apiVersion: extensions/v1beta1
                      kind: Deployment
                      metadata:
                      name: example
                      labels:
                      app: example
                      spec:
                      replicas: 1
                      selector:
                      matchLabels:
                      app: example
                      template:
                      metadata:
                      labels:
                      app: example
                      spec:
                      volumes:
                      - name: example-test-volume
                      secret:
                      secretName: example-test
                      containers:
                      - name: app
                      command: ["sleep", "99999999"]
                      image: busybox:latest
                      imagePullPolicy: IfNotPresent
                      volumeMounts:
                      - name: example-test-volume
                      mountPath: /tmp/example
                      env:
                      - name: exampleenv
                      valueFrom:
                      secretKeyRef:
                      name: example-test
                      key: example.txt


                      EOF





                      share|improve this answer




























                        0














                        It seems newlines work fine (maybe I ran into another issue earlier).



                        Here is a full example:



                        #!/usr/bin/env bash
                        set -euo pipefail

                        printf "123n456n789" > ./.work-in-progress/example.txt

                        kubectl create secret generic example-test
                        --from-file=./.work-in-progress/example.txt
                        --dry-run -o yaml | kubectl apply -f -

                        cat <<EOF | kubectl apply -f -
                        apiVersion: extensions/v1beta1
                        kind: Deployment
                        metadata:
                        name: example
                        labels:
                        app: example
                        spec:
                        replicas: 1
                        selector:
                        matchLabels:
                        app: example
                        template:
                        metadata:
                        labels:
                        app: example
                        spec:
                        volumes:
                        - name: example-test-volume
                        secret:
                        secretName: example-test
                        containers:
                        - name: app
                        command: ["sleep", "99999999"]
                        image: busybox:latest
                        imagePullPolicy: IfNotPresent
                        volumeMounts:
                        - name: example-test-volume
                        mountPath: /tmp/example
                        env:
                        - name: exampleenv
                        valueFrom:
                        secretKeyRef:
                        name: example-test
                        key: example.txt


                        EOF





                        share|improve this answer


























                          0












                          0








                          0







                          It seems newlines work fine (maybe I ran into another issue earlier).



                          Here is a full example:



                          #!/usr/bin/env bash
                          set -euo pipefail

                          printf "123n456n789" > ./.work-in-progress/example.txt

                          kubectl create secret generic example-test
                          --from-file=./.work-in-progress/example.txt
                          --dry-run -o yaml | kubectl apply -f -

                          cat <<EOF | kubectl apply -f -
                          apiVersion: extensions/v1beta1
                          kind: Deployment
                          metadata:
                          name: example
                          labels:
                          app: example
                          spec:
                          replicas: 1
                          selector:
                          matchLabels:
                          app: example
                          template:
                          metadata:
                          labels:
                          app: example
                          spec:
                          volumes:
                          - name: example-test-volume
                          secret:
                          secretName: example-test
                          containers:
                          - name: app
                          command: ["sleep", "99999999"]
                          image: busybox:latest
                          imagePullPolicy: IfNotPresent
                          volumeMounts:
                          - name: example-test-volume
                          mountPath: /tmp/example
                          env:
                          - name: exampleenv
                          valueFrom:
                          secretKeyRef:
                          name: example-test
                          key: example.txt


                          EOF





                          share|improve this answer













                          It seems newlines work fine (maybe I ran into another issue earlier).



                          Here is a full example:



                          #!/usr/bin/env bash
                          set -euo pipefail

                          printf "123n456n789" > ./.work-in-progress/example.txt

                          kubectl create secret generic example-test
                          --from-file=./.work-in-progress/example.txt
                          --dry-run -o yaml | kubectl apply -f -

                          cat <<EOF | kubectl apply -f -
                          apiVersion: extensions/v1beta1
                          kind: Deployment
                          metadata:
                          name: example
                          labels:
                          app: example
                          spec:
                          replicas: 1
                          selector:
                          matchLabels:
                          app: example
                          template:
                          metadata:
                          labels:
                          app: example
                          spec:
                          volumes:
                          - name: example-test-volume
                          secret:
                          secretName: example-test
                          containers:
                          - name: app
                          command: ["sleep", "99999999"]
                          image: busybox:latest
                          imagePullPolicy: IfNotPresent
                          volumeMounts:
                          - name: example-test-volume
                          mountPath: /tmp/example
                          env:
                          - name: exampleenv
                          valueFrom:
                          secretKeyRef:
                          name: example-test
                          key: example.txt


                          EOF






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 14:40









                          Chris StryczynskiChris Stryczynski

                          4,02653064




                          4,02653064






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297572%2fcan-kubernetes-secrets-store-newlines%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python