Skip to content
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

tctl: fix create cmd when input starts with an empty doc #51989

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tool/tctl/common/resource_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"log/slog"
"math"
"os"
"reflect"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -403,6 +404,14 @@ func (rc *ResourceCommand) Create(ctx context.Context, client *authclient.Client
}
return trace.Wrap(err)
}

// An empty document at the beginning of the input will unmarshal without error.
// Keep reading - there may be a valid document later on.
// https://github.com/gravitational/teleport/issues/4703
if reflect.ValueOf(raw).IsZero() {
continue
}

zmb3 marked this conversation as resolved.
Show resolved Hide resolved
count++

// locate the creator function for a given resource kind:
Expand Down
20 changes: 20 additions & 0 deletions tool/tctl/common/resource_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,10 @@ func TestCreateResources(t *testing.T) {
assert.Contains(t, s, "kind: user")
},
},
{
kind: "empty-doc",
create: testCreateWithEmptyDocument,
},
{
kind: types.KindDatabaseObjectImportRule,
create: testCreateDatabaseObjectImportRule,
Expand Down Expand Up @@ -1675,6 +1679,22 @@ spec:
require.NoError(t, err)
}

func testCreateWithEmptyDocument(t *testing.T, clt *authclient.Client) {
const userYAML = `
---
kind: user
version: v2
metadata:
name: llama2
spec:
roles: ["access"]
`
userYAMLPath := filepath.Join(t.TempDir(), "user.yaml")
require.NoError(t, os.WriteFile(userYAMLPath, []byte(userYAML), 0644))
_, err := runResourceCommand(t, clt, []string{"create", userYAMLPath})
require.NoError(t, err)
}

func testCreateUser(t *testing.T, clt *authclient.Client) {
// Ensure that our test user does not exist
_, err := runResourceCommand(t, clt, []string{"get", types.KindUser + "/llama", "--format=json"})
Expand Down
Loading