configuration-as-code-plugin icon indicating copy to clipboard operation
configuration-as-code-plugin copied to clipboard

Reading docker secret from file includes newline

Open MadsNielsen opened this issue 8 years ago • 11 comments

When reading from docker-secrets we read newlines as well. Example here is a file with the secret: secr3t

// https://mvnrepository.com/artifact/commons-io/commons-io
@Grapes(
    @Grab(group='commons-io', module='commons-io', version='2.5')
)

import org.apache.commons.io.FileUtils

final File file = new File("/var/deploy/secrets/adminpw")
String result = "'${FileUtils.readFileToString(file)}'"
groovy> // https://mvnrepository.com/artifact/commons-io/commons-io 
groovy> @Grapes( 
groovy>     @Grab(group='commons-io', module='commons-io', version='2.5') 
groovy> ) 
groovy> import org.apache.commons.io.FileUtils 
groovy> final File file = new File("/var/deploy/secrets/adminpw") 
groovy> String result = "'${FileUtils.readFileToString(file)}'" 
 
Result: 'secr3t
'

MadsNielsen avatar Apr 03 '18 08:04 MadsNielsen

Related to commit https://github.com/jenkinsci/configuration-as-code-plugin/commit/c83470c3b842358bbbbbb3f6af996dfa278953af

and our issues with reading the password in our demo setup: https://github.com/Praqma/praqma-jenkins-casc/issues/1

buep avatar Apr 03 '18 08:04 buep

Turns out im an idiot. The reason i raised this issue is that when i created the secrets with echo secr3t > /var/deploy/secrets/adminpw i didn't notice that the file actually has a newline.

You can manually edit out the newline from a proper editor and it works.

MadsNielsen avatar Apr 03 '18 10:04 MadsNielsen

Well I don't think you will be the only one overlooking the issue... if you look at our comment in https://github.com/Praqma/praqma-jenkins-casc/issues/1 we even did not fully understand that $ sign outputted from CAT -A command really is a newline.

I think other will face the same issue possible, so maybe we should write a warning if we see newline in the end of the input? White-space issues are really hard to debug often for users.

buep avatar Apr 03 '18 12:04 buep

@ewelinawilkosz @ndeloof what do you think? I'm not sure if you get the full context here, you might need to read up on the linked commits and issues.

But if the user add a newline to the file the password is wrong. Obviously, but it might be a problem several users face as some editor do it and not all of them shows it.

The .trim() is obviously not good I think.

Anyway not high priority, just some user feedback @MadsNielsen experienced himself.

buep avatar Apr 03 '18 12:04 buep

@MadsNielsen did you experienced this issue with actual docker-secrets or a file you created by yourself ?

ndeloof avatar Apr 03 '18 13:04 ndeloof

@ndeloof I did use docker secrets. With docker-compose though so not through docker swarm.

I.E like so:

version: '3.3'

services:
  jenkins: 
    build: 
      context: ./master
      args:
        JAVA_OPTS: '-Djenkins.CLI.disabled=true'
    ports: 
      - 80:8080
      - 50000:5000
    volumes:
      - jenkins_home:/var/jenkins_home
    secrets:
      - github
      - adminpw
secrets:
  github:
    file: /var/deploy/secrets/github
  adminpw:
    file: /var/deploy/secrets/adminpw
volumes:
  jenkins_home:

It's for our portable demo setup. But i think it might be an error 40 on my part.

MadsNielsen avatar Apr 03 '18 13:04 MadsNielsen

I was trying praqma's demo just before Easter and gave up on password not working, seems to be the issue you're describing here. And my comments are:

  1. not a highest prio since we have a solution - remove newline (am I right?) - but it would be good to mention somewhere in documentation
  2. I agree trim may not be the best final solution

ewelinawilkosz avatar Apr 04 '18 05:04 ewelinawilkosz

mentioning it in the documentation should be good enough

ewelinawilkosz avatar Jul 26 '18 14:07 ewelinawilkosz

afaict JCasC behaves as expected with real docker | kubernetes secrets. the fact manually backed "secret" files do introduce a trailing newline character is a PEBCAC problem we hardly can address automatically : how can we tell if secret is using a newline by intent or by mistake ?

ndeloof avatar Aug 09 '18 20:08 ndeloof

Any chance Jenkins modifies secret file while uploading via web interface? I have encountered strange problem when passing ssh priv key as secret file.

  1. using jenkins secret file it fails environment { SECRET_KEY= credentials('my-secret-key-id') } OpenSSL::PKey::PKeyError: Could not parse PKey: no start line
  2. using same container on same machine with same file as uploaded it works: docker run -e SECRET_KEY="$(cat my-secret-key-file)"

Im uploading from Windows while jenkins run on Linux. Tried both LF/CRLF with same result.

NVM find out that it actually contains path to a secret file in workspace.

ghost avatar Apr 06 '20 23:04 ghost

I was trying praqma's demo just before Easter and gave up on password not working, seems to be the issue you're describing here. And my comments are:

  1. not a highest prio since we have a solution - remove newline (am I right?) - but it would be good to mention somewhere in documentation
  2. I agree trim may not be the best final solution

@ewelinawilkosz how do u remove the newline ?

ns-mjames avatar May 29 '24 03:05 ns-mjames