MCPcopy Create free account
hub / github.com/docker/cli / readSecretData

Function readSecretData

cli/command/secret/create.go:126–161  ·  view source on GitHub ↗

500KB readSecretData reads the secret from either stdin or the given fileName. It reads up to twice the maximum size of the secret ([maxSecretSize]), just in case swarm's limit changes; this is only a safeguard to prevent reading arbitrary files into memory.

(in io.Reader, fileName string)

Source from the content-addressed store, hash-verified

124// just in case swarm's limit changes; this is only a safeguard to prevent
125// reading arbitrary files into memory.
126func readSecretData(in io.Reader, fileName string) ([]byte, error) {
127 switch fileName {
128 case "-":
129 data, err := io.ReadAll(io.LimitReader(in, 2*maxSecretSize))
130 if err != nil {
131 return nil, fmt.Errorf("error reading from STDIN: %w", err)
132 }
133 if len(data) == 0 {
134 return nil, errors.New("error reading from STDIN: data is empty")
135 }
136 return data, nil
137 case "":
138 return nil, errors.New("secret file is required")
139 default:
140 // Open file with [FILE_FLAG_SEQUENTIAL_SCAN] on Windows, which
141 // prevents Windows from aggressively caching it. We expect this
142 // file to be only read once. Given that this is expected to be
143 // a small file, this may not be a significant optimization, so
144 // we could choose to omit this, and use a regular [os.Open].
145 //
146 // [FILE_FLAG_SEQUENTIAL_SCAN]: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#FILE_FLAG_SEQUENTIAL_SCAN
147 f, err := sequential.Open(fileName)
148 if err != nil {
149 return nil, fmt.Errorf("error reading from %s: %w", fileName, err)
150 }
151 defer f.Close()
152 data, err := io.ReadAll(io.LimitReader(f, 2*maxSecretSize))
153 if err != nil {
154 return nil, fmt.Errorf("error reading from %s: %w", fileName, err)
155 }
156 if len(data) == 0 {
157 return nil, fmt.Errorf("error reading from %s: data is empty", fileName)
158 }
159 return data, nil
160 }
161}

Callers 1

runSecretCreateFunction · 0.85

Calls 1

CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…