-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tctl] Adds option to write tarred
tctl auth sign
output to stdout (#…
…29451) * Proof-of-concept writing tar to stdout A quick and dirty experiment showing one possible approach for writing certificates to stdout. Demonstrates a possible solution to #29262. DO NOT MERGE AS IS. IN NO WAY PRODUCTION READY. * Fix timestamps * Adds `--tar` option to `auth sign` Adds an option to bundle the certificates generated by `tctl auth sign` into a tarball and writes that tarball to stdout. This is to facilitate extracting credentials from environments with limited access to the filesystem and tools like a shell, tar and so on, e.g. distroless Docker images. Example usage: ``` $ kubectl exec ... -- tctl auth sign --user alice --format openssh -o alice --tar | tar xv x alice-cert.pub x alice ``` * Reroutes helper mesg to stderr when straming tar file to stdout
- Loading branch information
Showing
4 changed files
with
280 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2023 Gravitational, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package common | ||
|
||
import ( | ||
"archive/tar" | ||
"io" | ||
"io/fs" | ||
"os" | ||
|
||
"github.com/gravitational/trace" | ||
"github.com/jonboulle/clockwork" | ||
|
||
"github.com/gravitational/teleport/lib/client/identityfile" | ||
) | ||
|
||
// tarWriter implements a ConfigWriter that generates a tarfile from the | ||
// files written to the config writer. Does not implement | ||
type tarWriter struct { | ||
tarball *tar.Writer | ||
clock clockwork.Clock | ||
} | ||
|
||
// newTarWriter creates a new tarWriter that writes the generated tar | ||
// file to the supplied `io.Writer`. Be sure to terminate the tar archive | ||
// by calling `Close()` on the resuting `tarWriter`. | ||
func newTarWriter(out io.Writer, clock clockwork.Clock) *tarWriter { | ||
return &tarWriter{ | ||
tarball: tar.NewWriter(out), | ||
clock: clock, | ||
} | ||
} | ||
|
||
// Remove is not implemented, and only exists to fill out the | ||
// `ConfigWriter` interface. | ||
func (t *tarWriter) Remove(_ string) error { | ||
return trace.NotImplemented("tarWriter.Remove()") | ||
} | ||
|
||
// Stat always returns `ErrNotExist` in ordre to sidestep the | ||
// overwite check when writing certificates via a ConfigWriter. | ||
func (t *tarWriter) Stat(_ string) (fs.FileInfo, error) { | ||
return nil, os.ErrNotExist | ||
} | ||
|
||
// WriteFile adds the supplied content to the tar archive. | ||
func (t *tarWriter) WriteFile(name string, content []byte, mode fs.FileMode) error { | ||
header := &tar.Header{ | ||
Name: name, | ||
Mode: int64(mode), | ||
ModTime: t.clock.Now(), | ||
Size: int64(len(content)), | ||
} | ||
if err := t.tarball.WriteHeader(header); err != nil { | ||
return trace.Wrap(err) | ||
} | ||
if _, err := t.tarball.Write(content); err != nil { | ||
return trace.Wrap(err) | ||
} | ||
return nil | ||
} | ||
|
||
// Close finalizes the tar archive, adding any necessary padding and footers. | ||
func (t *tarWriter) Close() error { | ||
return trace.Wrap(t.tarball.Close()) | ||
} | ||
|
||
// identityfile.ConfigWriter implementation check | ||
var _ identityfile.ConfigWriter = (*tarWriter)(nil) |
Oops, something went wrong.