Repository navigation
Description
Background
While trying to implement examples for issue Allow directories to be mounted as secrets, it became apparent that there are limitations when dealing with binary data in GraphQL using strings. This is relevant for the purpose of mounting directories as secrets, where binary data needs to be handled.
I encountered a problem when attempting to handle GPG files. The goal was to create and GPG-sign a built binary with Dagger by mounting the ~/.gnupg directory into the container and running gpg --detach-sign --armor binary-name.
This required the following files:
- ~/.gnupg/pubring.kbx
- ~/.gnupg/trustdb.gpg
- ~/.gnupg/private-keys-v1.d/*
Handling binary data like GPG files in GraphQL led to errors due to improper escaping:
go run .
Connected to engine c01f0fd17590
panic: input:1: Syntax Error GraphQL request (1:49) Invalid character escape sequence: \\x.The initial idea was to implement a union type SecretData = String | Bytes in the setSecret query. However, according to the discussion, using unions as input values in GraphQL is not feasible.
Issue
The main issue is finding a solution to handle both string and binary data when setting secrets in GraphQL, as the initial proposal of using a union type is not possible. This is important for mounting directories as secrets and handling binary data efficiently.
Proposed Solution
Based on the comments and limitations mentioned in the discussion, the best solution seems to be to create a new function that accepts binary data as Base64 encoded string as input. This function would be a variation of withSecretVariable and handle binary data separately.
Advantages
- No character encoding issues: By using Base64 encoding, we can ensure that all binary data is properly serialized and deserialized without any issues related to character encoding.
- Efficient data transfer: Base64-encoded binary data is more compact than a string representation, reducing the overhead associated with transferring binary data in GraphQL queries or mutations.
- Clear type information: Defining a custom type allows us to be explicit regarding the expected encoded format
Steps to implement the solution:
- Create a new custom type
Base64in the GraphQL schema to handle binary data.
type Base64 string- Define a new function in the client SDK called WithSecretBytes that accepts an environment variable name and Base64 data as input.
func (c *Container) setSecretBytes(name string, data Base64) *Container- Update the GraphQL schema to include the new function setSecretBytes.
extend type Query {
"Sets a secret given a user-defined name to its binary data and returns the secret."
setSecretBytes(
"""
The user-defined name for this secret
"""
name: String!
"""
The binary data of the secret
"""
data: Base64!
): Secret!
}- Implement deserialization functions for the Base64
setSecretBytes, handling the decoding of binary from Base64. - Update the SDK example to show how to convert binary data to Base64-encoded strings before sending them as secret arguments.
By implementing this new function, we can effectively handle binary data in GraphQL, making it possible to mount directories as secrets in a more convenient and efficient manner.
Activity
helderco commentedon May 2, 2023
Have you considered doing something like
Host.Directory()? So instead ofdata: Base64!, you'd usepath: String!.grouville commentedon May 2, 2023
We can also introduce a new parameter to the
setSecretquery that specifies the encoding format. This would allow us to handle different encoding formats more explicitly:vito commentedon May 2, 2023
I think this is just because we took a shortcut in our query builder by assuming GraphQL strings follow the same rules as Go: #5008 (comment)
We should probably update the query builders to follow the GraphQL spec instead. I think with that change we shouldn't need to support alternative encodings, but I'm still in support of the approach in #5069 (comment) if we want to go that route.
Worth noting GraphQL string escaping is something that would have to be implemented properly for every SDK. I'm not sure if this issue applies to the other SDKs.
grouville commentedon May 2, 2023
Totally agree, thanks for the insight 🙏
Tested on Node SDK, it seems to work for one of the file on which the Go SDK breaks. I still have an issue on another file (bigger binary data), don't know if it is related or not. But I presume that the implementation is somewhat similar:
invalid secret size 514282. The escaping might be the root cause here too:grouville commentedon May 4, 2023
I have explored our graphQL implementation. Currently only have a partial implementation of the spec, still some issues on non-printable unicode chars. All the experiments are available here.
In theory, what Alex proposes is possible, still haven't found the implementation yet.
Next step:
Gist/experiment with links
grouville commentedon May 22, 2023
Here is a quick overview of where the issue lies, what the arbitrages are and potential next steps to its resolution.
Global problem
When trying to mount binary files into Dagger, we encounter encoding issues on invalid UTF-8 sequences.
Repro: https://gist.github.com/grouville/df32b261dcf8bf1a9c7d140f5c18a09b
This gives us the issue:
1. Isolating the root issue
It turns out that the issue lies in the dagger/graphql lib that we forked. My first attempt was to follow Alex’s advice to try understanding how / if we could manage this issue at the level of the library.
1. The issue arises at the library level
This is the repro showing that the library doesn’t handle properly the unicodes. Here, I create a new scalar, with a corresponding query handle, but we don’t even get at the resolve level. We get the same error as the one we get from the Dagger Go SDK:
2. Implementing the GraphQL spec
As @vito suggested, we explored if there was a way to implement the graphQL’s spec on unicode management. It could theoretically work, but I haven’t found a sustainable way to retrieve the exact data I sent that way:
1. gqlgen strings marshalling
gqlgenimplemented a marshalling / unmarshalling of strings compliant with the graphQL specs.2. Testing gqlgen strings marshalling as a solution
The idea here is to marshal the binary data and see if we have the exact same bytes data at the server level, and as a response. The test string can be one of the following breaking test:
Let’s take:
"\u2764"as an example.The two repros show that:
First run, with
no_escape.go:Second run, with
string_escape.go:3. JSON Unmarshalling is not unicode safe, in Go
However, JSON unmarshalling of some unicode bytes break in Go, and we do rely on that to unmarshall the server response.
Let's take the almost the same code as above (with gqlgen strings escape), and let's try to load my binary gpg key and see if my output data is equivalent to my input data: code here.
go run ./main.go Incoming data matches original data OUTPUT data does not match original data!What is interesting here is that the 'incoming data', in the server is equivalent to the original data. However, when retrieving it on the response, the response data doesn't match the original data.
What is happening ?
It turns out that this all comes from the:
Here are the repros: https://gist.github.com/grouville/8d8ac47ba16e7980e9edca797492eeaf
stdlib_json.go, breaks on my gpg key file:The idea was to rely on https://github.com/nspcc-dev/go-ordered-json. This library is being used in several blockchains for two purposes: json unmarshal ordering and serialization of unicode bytes. It should manage this issue.
However, it doesn't work on my example: my local test doesn't pass either. Check
forked_json.gofor the repro:Now, we are sure that the gqlgen strings escape works, however, there is an issue with the JSON unmarshalling. We do rely on that in our codebase (at least in Go SDK) to process the server's response: https://github.com/dagger/dagger/blob/main/sdk/go/internal/querybuilder/querybuilder.go#L121C10-L148.
3. A Base64 Bytes scalar
Now, using base64, JSON unmarshal seems to not only work perfectly, but seems to be easily handling base64 encoded strings. In the
base64.goexample, I don't decode the base64 string:With the complete scalar example, relying on a base64 encoding also works perfectly: https://gist.github.com/grouville/ae31ef61a192d9afc55334f220d2db3f.
We could even go further to automatically decode the base64 response with a custom json umarshaller:
2. Proposal
Given all this context, and taking into account the fact that I did not miss anything, I see 2 possible implementations, to be defined with you @helderco.
In this issue, and according to the tests, it can be split in 2 sub-issues:
Basically, this probably only implies to transform the
strconv.Quoteto the gqlgen strings escape (for the Go SDK only), and do the same for other SDKs. It will make us handle unicodes for 99% of use-cases on our SDKs, and is automatically escaped server-side.I see 2 options, with big implications:
[]byte{}server-sideString. The API would have aencodingfield.For both cases, this seems to be a breaking change in the API, but following all exploration above, I don't think a retro-compatible solution is doable ... I would love so, but don't think going 100% strings is what we want here 👼
What do you think @helderco ? What would be the best way to handle both scalar types on the API ?
cc @matiasinsaurralde @vito @aluzzardi @helderco
[-]Handling Binary Data as Secrets[/-][+]Handling Binary Data[/+]helderco commentedon May 22, 2023
@grouville Can you test a repro for the other SDKs failing to properly encode the strings? Python uses a third party library for the query builder while node.js has a custom implementation. Maybe they don't have an issue here?
grouville commentedon May 30, 2023
You're right, other SDKs do not have this issue for UTF-8 unicodes, but the issue remains for binary files with weird sequences, as explained above. Repros are present on PR: #5169
@sipsma, after talking with @helderco, it seems that we used to have a way to mount secret files inside containers, with a Dagger equivalent to "fsync". If possible, this would enable us to create a new API endpoint to mount a secret file from a path, instead of reading its content as text. Do you believe this is possible ?
Otherwise, the base64 approach will have to be followed
helderco commentedon May 30, 2023
@grouville To be clear, when I mentioned fsync I believe it's a fork we use to sync files between the client and the engine (don't know internals, just remember bits of discussions).
That's different from the deprecated secret file implementation which is just using
File.secret(), thus getting leaked in the cache via any of the ways you can get aFile. When coming fromHost.directory, that's usingllb.Local.What I suggested was that maybe via the lower level fsync, we can send a file from the client to the engine in a way that remains secret to buildkit, so we can add it to the in-memory store.
helderco commentedon May 30, 2023
By the way I don’t think you need to reach for the lower level fsync directly. If your argument is a path to a file, then in the API you just open it in Go, add it to the in-memory secret store and return a
SecretID, like a mix ofHost.directoryandQuery.setSecret:helderco commentedon May 30, 2023
@grouville Fyi, I just tested the above and it works. Basically creating a new api field that receives a path, and reading the content with Go, without going to buildkit. From there, it's just adding to the secret store.
helderco commentedon May 30, 2023
Re: binary files... I suggest changing the store's data type in
core/store.go:type Store struct { - secrets map[string]string + secrets map[string][]byte }So that
GetSecretreturns the original[]byteuntouched.Also adapt
AddSecretto receive[]byteand do the string conversion where it's actually needed likeQuery.setSecret()andSecret.plaintext().2 remaining items
gerhard commentedon Jul 18, 2023
We (+@helderco) would like to get this done for:
WDYT @grouville?
grouville commentedon Jul 18, 2023
Totally agree. Prioritizing it 🙏
feat: implement host.setSecretFile() method to securely mount binary …
feat: implement `host.setSecretFile()` to securely set binary files a…
feat: implement `host.setSecretFile()` to securely set binary files a…