-
Notifications
You must be signed in to change notification settings - Fork 462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support reading JSON data from STDIN #1905
Conversation
138265c
to
0646577
Compare
parameter
file
stdin
update
|
42bf8d7
to
aa95332
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my side just some minor wording suggestions. Thank you for the work!
Otherwise LGTM
cmd/spire-server/cli/entry/create.go
Outdated
@@ -233,7 +218,7 @@ func (CreateCLI) newConfig(args []string) (*CreateConfig, error) { | |||
f.StringVar(&c.SpiffeID, "spiffeID", "", "The SPIFFE ID that this record represents") | |||
f.IntVar(&c.TTL, "ttl", 0, "The lifetime, in seconds, for SVIDs issued based on this registration entry") | |||
|
|||
f.StringVar(&c.Path, "data", "", "Path to a file containing registration JSON (optional)") | |||
f.StringVar(&c.Path, "data", "", "Path to a file containing registration JSON (optional). If set '-' as file path, read the JSON passed into stdin.") |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
cmd/spire-server/cli/entry/update.go
Outdated
@@ -219,7 +204,7 @@ func (UpdateCLI) newConfig(args []string) (*UpdateConfig, error) { | |||
f.StringVar(&c.SpiffeID, "spiffeID", "", "The SPIFFE ID that this record represents") | |||
f.IntVar(&c.TTL, "ttl", 0, "The lifetime, in seconds, for SVIDs issued based on this registration entry") | |||
|
|||
f.StringVar(&c.Path, "data", "", "Path to a file containing registration JSON (optional)") | |||
f.StringVar(&c.Path, "data", "", "Path to a file containing registration JSON (optional). If set '-' as file path, read the JSON passed into stdin.") |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
doc/spire_server.md
Outdated
@@ -229,7 +229,7 @@ Creates registration entries. | |||
| Command | Action | Default | | |||
|:-----------------|:-----------------------------------------------------------------------|:---------------| | |||
| `-admin` | If set, the SPIFFE ID in this entry will be granted access to the Registration API | | | |||
| `-data` | Path to a file containing registration data in JSON format (optional). | | | |||
| `-data` | Path to a file containing registration data in JSON format (optional). If set '-' as file path, read the JSON passed into stdin. | | |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
doc/spire_server.md
Outdated
@@ -248,7 +248,7 @@ Updates registration entries. | |||
| Command | Action | Default | | |||
|:-----------------|:-----------------------------------------------------------------------|:---------------| | |||
| `-admin` | If true, the SPIFFE ID in this entry will be granted access to the Registration API | | | |||
| `-data` | Path to a file containing registration data in JSON format (optional). | | | |||
| `-data` | Path to a file containing registration data in JSON format (optional). If set '-' as file path, read the JSON passed into stdin. | | |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
cmd/spire-server/cli/entry/util.go
Outdated
@@ -91,6 +95,34 @@ func printEntry(e *common.RegistrationEntry) { | |||
fmt.Println() | |||
} | |||
|
|||
// ParseFile parses JSON represented RegistrationEntries | |||
// if path is "" or "*" read JSON from STDIN | |||
func ParseFile(in io.Reader, path string) ([]*common.RegistrationEntry, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I think this can be made to:
func ParseFile(path string) ([]*common.RegistrationEntry, error) {
return parseFile(os.Stdin, path)
}
func parseFile(stdin io.Reader, path string) ([]*common.RegistrationEntry, error) {
...
}
This provides a nice public API (the consumer doesn't need to pass os.Stdin
, which is somewhat of an implementation detail), which also giving you the testability I believe you're looking for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks good!
6096a68
cmd/spire-server/cli/entry/util.go
Outdated
@@ -91,6 +95,34 @@ func printEntry(e *common.RegistrationEntry) { | |||
fmt.Println() | |||
} | |||
|
|||
// ParseFile parses JSON represented RegistrationEntries | |||
// if path is "" or "*" read JSON from STDIN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace "*"
with "-"
Also, I'm not confident we want to alias an empty path as stdin.. as it could mask user error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the empty path doesn't work well.
The comment seems to have just been forgotten to remove.
6096a68
cmd/spire-server/cli/entry/util.go
Outdated
|
||
var r io.Reader | ||
if path == "-" { | ||
r = in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be simplified to remove the else
with something like:
r := stdin
if path != "-" {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
r = f
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6096a68
7cd898b
to
ce0929b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but a few questions on the tests
cmd/spire-server/cli/entry/util.go
Outdated
@@ -91,6 +95,36 @@ func printEntry(e *common.RegistrationEntry) { | |||
fmt.Println() | |||
} | |||
|
|||
// ParseFile parses JSON represented RegistrationEntries | |||
// if path is "-" read JSON from STDIN | |||
func ParseFile(path string) ([]*common.RegistrationEntry, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this actually doesn't need to be a public method, since it's not used outside of this package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, exactly.
587ac9c
assert.NoError(t, err) | ||
// Replace os.Stdin temporary | ||
orgStdin := os.Stdin | ||
os.Stdin = fakeStdin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer not to override the os.Stdin
package global in tests.
One option is to use the dependency injection and test the parseFile(io.Reader, filename)
function and leave ParseFile(filename)
without directly testing it.
Another option is to set var stdin = os.Stdin
and then replace the package local stdin
var, which is much less invasive than mutating os.Stdin
entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
587ac9c
This commit changes the target and tests the parseFile(io.Reader, filename)
function.
@@ -45,3 +49,93 @@ func selectorToFlag(t *testing.T, selectors []*common.Selector) StringsFlag { | |||
|
|||
return resp | |||
} | |||
|
|||
func TestRegisterParseFile(t *testing.T) { | |||
p := path.Join(util.ProjectRoot(), "test/fixture/registration/good.json") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any bad json we should test against?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
587ac9c
Invalid JSON is now also included in the test case.
Also refactored test functions.
587ac9c
to
bd3d93e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Thank you for this feature @hiyosi! We need to rebase the PR to get it merged in. Would you mind either rebasing/merging master or allowing us to do it? Thanks again! |
bd3d93e
to
610fb24
Compare
Signed-off-by: Tomoya Usami <tousami@zlab.co.jp> fixup! Support reading JSON data from STDIN Signed-off-by: Tomoya Usami <tousami@zlab.co.jp> fixup! fixup! Support reading JSON data from STDIN Signed-off-by: Tomoya Usami <tousami@zlab.co.jp>
610fb24
to
6300e57
Compare
@hiyosi Thank you very much for your effort! Supremely appreciated over here. |
Signed-off-by: Tomoya Usami tousami@zlab.co.jp
Pull Request check list
Affected functionality
Server CLI: entry create/update
Description of change
entry create/update commands accept JSON data from STDIN.
Which issue this PR fixes
#1864