#Secrets in the CLI

1 messages ยท Page 1 of 1 (latest)

sage raven
#

Hey, the built-in secrets exist in memory, so you need to run those queries in the same dagger session. Multiple dagger query commands will trigger new sessions.

sage raven
#

In the meantime you can use curl with dagger run sh ....

little marlin
sage raven
#

If query reuses an existing session then you can just run the script with dagger run, e.g., dagger run sh -c 'dagger query ...'.

#

Either way, maybe you have a different source for secrets that doesn't depend on setSecret. We have plans to make this easier by being able to swap the secret store for another (e.g., 1Password), in the future.

sage raven
#

@void wing, try this as a workaround: dagger run script.sh, with following:

#!/bin/sh

set -euo pipefail

query() {
    jq -sRn '{query: input}' | \
    curl -s \
        -u $DAGGER_SESSION_TOKEN: \
        -H "content-type:application/json" \
        -d@- http://127.0.0.1:$DAGGER_SESSION_PORT/query | \
    jq -r .data
}

S_APP_ID=$(query <<EOF | jq -r .setSecret.id
{
  setSecret(
        name: "APP_ID",
        plaintext: "1234"
    ) {
        id
    }
}
EOF
)

output=$(query <<EOF | jq -r .container.from.withSecretVariable.withExec.stdout
{
  container {
    from(address: "node:latest") {
      withSecretVariable(name: "APP_ID", secret: "$S_APP_ID") {
        withExec(args: ["-v"]) {
          stdout
        }
      }
    }
  }
}
EOF
)

echo $output
void wing
#

Thanks for the workaround..it works..

void wing
#

what could be the reliable solution in future apart from workaround?

sage raven
void wing
# sage raven Wouldn't https://github.com/dagger/dagger/issues/5181 work for you? You'd be abl...

yes once #5181 available that would work. meanwhile we are trying to use curl to query dagger with node as suggested. However in js file we are not getting correct values of secrets. can you please help us with that?

#** setup.sh** file for setting up secrets and running test.js in the container

#!/bin/bash
set -euo pipefail

query() {
    jq -sRn '{query: input}' | \
    curl -s \
        -u $DAGGER_SESSION_TOKEN: \
        -H "content-type:application/json" \
        -H "X-Debug:true" \
        -d@- http://127.0.0.1:$DAGGER_SESSION_PORT/query | \
    jq -r .data
}


S_APP_ID=$(query <<EOF | jq -r .setSecret.id
{
  setSecret(
        name: "APP_ID",
        plaintext: "123"
    ) {
        id
    }
}
EOF
)
echo "S_APP_ID: $S_APP_ID"

# scripts directory contains test.js file.
S_SOURCE_DIR=$(query <<EOF | jq -r .host.directory.id
{
  
        host{
            directory(path: "scripts"){
                id
            }
        }
}
EOF
)

output=$(query <<EOF | jq -r .container.from.withDirectory.withWorkdir.withSecretVariable.withEntrypoint.stdout
{
  container {
    from(address: "node:latest") {
      withDirectory(directory: "$S_SOURCE_DIR", path: "/") {
        withWorkdir(path: "/") {
          withSecretVariable(name: "APP_ID", secret: "$S_APP_ID") {
            withEntrypoint(args: ["node", "test.js"]) {
              stdout
            }  
          }
        }
      }
    }
  }
}
EOF
)

echo $output

test.js file for testing the secrets


console.log("Hello world");
console.log(process.env.APP_ID);

This is the output we are getting from the container when we run it using dagger run ./setup.sh. Getting filenames instead of secrets values.

Hello world
package.json setup.sh ...
sage raven
#

Seems to work for me, but may I suggest you change withEntrypoint to withExec? Notice that the command that is run is node test.js node because node is in CMD, which in Dagger it's equal to withDefaultArgs to the entrypoint. Switching to withExec will replace node as the default command to using yours.

#

The this becomes ๐Ÿ‘‡

void wing
#

I tried ith withExec and it returns failed to solve: fork/exec ./setup.sh: exec format error

sage raven
void wing
#

dagger v0.5.2 darwin/arm64

sage raven
#

One sec while I test.

void wing
#

how can we add debug in curl? like we do --debug in dagger query in cli..can we do it in curl command?

sage raven
#

You can do it dagger run --debug script.sh.

void wing
#

yes but it is not debugging the query...its just adding connected to engine log and directly running the curl..

Connected to engine a20ef2dfeac9
failed to solve: fork/exec ./setup.sh: exec format error
sage raven
#

The TUI does that: _EXPERIMENTAL_DAGGER_TUI=1 dagger run --debug script.sh

#

Can you show me your script as it's now?

void wing
#
# setup.sh file for setting up secrets and running the container with test.js
#!/bin/bash
set -euo pipefail

query() {
    jq -sRn '{query: input}' | \
    curl -s \
        -u $DAGGER_SESSION_TOKEN: \
        -H "content-type:application/json" \
        -H "X-Debug:true" \
        -d@- http://127.0.0.1:$DAGGER_SESSION_PORT/query | \
    jq -r .data
}


S_APP_ID=$(query <<EOF | jq -r .setSecret.id
{
  setSecret(
        name: "APP_ID",
        plaintext: "123"
    ) {
        id
    }
}
EOF
)
echo "S_APP_ID: $S_APP_ID"


S_SOURCE_DIR=$(query <<EOF | jq -r .host.directory.id
{
  
        host{
            directory(path: "scripts"){
                id
            }
        }
}
EOF
)

output=$(query <<EOF | jq -r .container.from.withDirectory.withWorkdir.withSecretVariable.withExec.stdout
{
  container {
    from(address: "node:latest") {
      withDirectory(directory: "$S_SOURCE_DIR", path: "/") {
        withWorkdir(path: "/") {
          withSecretVariable(name: "APP_ID", secret: "$S_APP_ID") {
            withExec(args: ["node", "test.js"]) {
              stdout
            }  
          }
        }
      }
    }
  }
}
EOF
)

echo $output

Executing _EXPERIMENTAL_DAGGER_TUI=1 dagger run --debug ./setup.sh

โ–ˆ ERROR ./setup.sh
โ”ป 
fork/exec ./setup.sh: exec format error
sage raven
#

Still works for me... maybe you have some char in the file that I don't get when copying and pasting? Can you send me the file as an attachment?

#

And also, try updating to 0.5.3. That's what I'm testing on (latest release).

void wing
#

yes i just upgraded version but no luck..yes sending you script as file just a min

#

updated to 0.5.3 dagger v0.5.3 darwin/arm64

sage raven
#

Now I can reproduce ๐Ÿ™‚

void wing
#

thats good ๐Ÿ™‚

sage raven
#

Ah, you need to remove that first comment. The first line has to be the #!/bin/bash bang.

#

You had that when you last pasted but I unconsciously left it out of my copy.

void wing
#

ohh..yaa now command is running...but are you able to see console logs of test.js in terminal? what is the secret value do you see when you execute dagger run ./setup.sh

sage raven
#

It returns: Hello world setup.sh testscript.

#

Ok, let me check

void wing
#

yes thats the issue...not getting the actual secret value in js...process.env shows env APP_ID but when we print it in console it is printing filenames

sage raven
#

Just remember that dagger's logs will strip out the secret when you print it to avoid leaks, but you should be getting *** instead.

void wing
#

yes thats true...if we run it without TUI dagger run ./setup.sh then it is printing filenames.. and if we run it with TUI _EXPERIMENTAL_DAGGER_TUI=1 dagger run ./setup.sh then it is printing nothing..not even Hello world.

sage raven
#

That's because it's cached, more likely. In the next release it'll show the output.

void wing
#

how we can make sure that js file is getting the correct values? can we debug it or store env in file through js?

sage raven
#

You can use -i with the TUI for the interactive version.

sage raven
#

Give me a sec to debug it.

#

I was getting the *** before, there must be something different in this version of your script.

void wing
#

sure

sage raven
#

I need to put a cache buster in there

#

It's making it harder to iterate ๐Ÿ™‚

#

Yeah, I'm getting it now. So it must be your cache.

#
#!/bin/bash
set -euo pipefail

query() {
    jq -sRn '{query: input}' | \
    curl -s \
        -u $DAGGER_SESSION_TOKEN: \
        -H "content-type:application/json" \
        -H "X-Debug:true" \
        -d@- http://127.0.0.1:$DAGGER_SESSION_PORT/query | \
    jq -r .data
}


S_APP_ID=$(query <<EOF | jq -r .setSecret.id
{
  setSecret(
        name: "APP_ID",
        plaintext: "123"
    ) {
        id
    }
}
EOF
)
echo "S_APP_ID: $S_APP_ID"


S_SOURCE_DIR=$(query <<EOF | jq -r .host.directory.id
{

        host{
            directory(path: "testscript"){
                id
            }
        }
}
EOF
)

output=$(query <<EOF | jq -r .container.from.withEnvVariable.withDirectory.withWorkdir.withSecretVariable.withExec.stdout
{
  container {
    from(address: "node:latest") {
    withEnvVariable(name: "CACHE_BUSTER", value: "$(date)") {
      withDirectory(directory: "$S_SOURCE_DIR", path: "/") {
        withWorkdir(path: "/") {
          withSecretVariable(name: "APP_ID", secret: "$S_APP_ID") {
            withExec(args: ["node", "test.js"]) {
              stdout
            }
          }
        }
      }
    }}
  }
}
EOF
)

echo $output
#

I added the "CACHE_BUSTER" env var.

#

This prevents the next steps from getting cached.

#

So, the exec is returning correctly

#

It seems to be the setup.sh that's returning the files.

#

$output may have a special meaning in the shell.

#

Change to TEST_OUT or something like that

void wing
#

I copied your script but in my pc its giving me the same issue...have you just passed CACHE_BUSTER in graphql query or did anything extra in cli? I also tried changing output to TEST_OUT but the same prb

sage raven
#

It's actually this:

-echo $output
+echo "$output"
#
console.log("Hello world")
if (process.env.APP_ID === "123") {
    console.log("You got it!")
} else {
    console.log("Oh noes!!")
}
โฏ dagger run ./setup.sh
S_APP_ID: eyJuYW1lIjoiQVBQX0lEIn0=
Hello world
You got it!
void wing
#

yes finally if i run dagger run ./setup.sh then it is giving me exact same output as yours...but with TUI I m not getting any console logs like yours..may be it is due to cache. I also tried with changing name of test.js file..but with _EXPERIMENTAL_DAGGER_TUI=1 dagger run ./setup.sh command it is not printing logs in cli..

sage raven
#

I'm showing the interactive TUI... use dagger run -i ....

void wing
#

yes got it with interactive TUI -i... and S_APP_ID is giving same secret id everytime...I am also getting "eyJuYW1lIjoiQVBQX0lEIn0=" for S_APP_ID ..not sure it is the expected behaviour or not..anyway thanks a lot for your time and help ๐Ÿ™‚

sage raven
#

Yeah, it's expected, it's based on the key, not the value ๐Ÿ™‚

void wing
#

cool!!

#

one more question.. how we should clear the cache periodically in dagger?

sage raven
#
โฏ echo "eyJuYW1lIjoiQVBQX0lEIn0=" | base64 -d
{"name":"APP_ID"}
sage raven
void wing
#

dagger manage caching in pc right? so what is the best solution to save disk space ? can we clear cached images or files during the dagger run periodically?

sage raven
#

Not selectively. You can periodically wipe the whole cache, meaning the next run will be slower but that would solve your disk space issue.

void wing
#

okay got it...thanks!!

void wing
#

@sage raven hi there?

sage raven
void wing
#

hi there?