Files
nomad/helper/file.go
James Rasell 80a26306bf intro: Add node introduction flow for Nomad client registration. (#26405)
This change implements the client -> server workflow for Nomad
node introduction. A Nomad node can optionally be started with an
introduction token, which is a signed JWT containing claims for
the node registration. The server handles this according to the
enforcement configuration.

The introduction token can be provided by env var, cli flag, or
by placing it within a default filesystem location. The latter
option does not override the CLI or env var.

The region claims has been removed from the initial claims set of
the intro identity. This boundary is guarded by mTLS and aligns
with the node identity.
2025-08-05 08:23:44 +01:00

49 lines
1.1 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package helper
import (
"io"
"os"
)
// ReadFileContent is a helper that mimics the stdlib ReadFile implementation,
// but accepts an already opened file handle. This is useful when using os.Root
// functionality such as OpenInRoot which does not have convenient read methods.
func ReadFileContent(file *os.File) ([]byte, error) {
var size int
if info, err := file.Stat(); err == nil {
size64 := info.Size()
if int64(int(size64)) == size64 {
size = int(size64)
}
}
size++ // one byte for final read at EOF
// If a file claims a small size, read at least 512 bytes. In particular,
// files in Linux's /proc claim size 0 but then do not work right if read in
// small pieces, so an initial read of 1 byte would not work correctly.
if size < 512 {
size = 512
}
data := make([]byte, 0, size)
for {
n, err := file.Read(data[len(data):cap(data)])
data = data[:len(data)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return data, err
}
if len(data) >= cap(data) {
d := append(data[:cap(data)], 0) //nolint:gocritic
data = d[:len(data)]
}
}
}