Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: cyli <[email protected]>
  • Loading branch information
cyli committed Sep 27, 2016
1 parent 525fc27 commit 8777042
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cmd/swarmctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/docker/swarmkit/cmd/swarmctl/cluster"
"github.com/docker/swarmkit/cmd/swarmctl/network"
"github.com/docker/swarmkit/cmd/swarmctl/node"
"github.com/docker/swarmkit/cmd/swarmctl/secrets"
"github.com/docker/swarmkit/cmd/swarmctl/secret"
"github.com/docker/swarmkit/cmd/swarmctl/service"
"github.com/docker/swarmkit/cmd/swarmctl/task"
"github.com/docker/swarmkit/version"
Expand Down Expand Up @@ -55,6 +55,6 @@ func init() {
version.Cmd,
network.Cmd,
cluster.Cmd,
secrets.Cmd,
secret.Cmd,
)
}
2 changes: 1 addition & 1 deletion cmd/swarmctl/secrets/cmd.go → cmd/swarmctl/secret/cmd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package secrets
package secret

import "github.com/spf13/cobra"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package secrets
package secret

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package secrets
package secret

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package secrets
package secret

import (
"errors"
"fmt"
"os"
"text/tabwriter"
"time"

"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/cmd/swarmctl/common"
"github.com/docker/swarmkit/protobuf/ptypes"
"github.com/spf13/cobra"
)

Expand All @@ -27,8 +27,7 @@ func printSecretSummary(secret *api.Secret) {
common.FprintfIfNotEmpty(w, "Digest\t: %s\n", secret.Digest)
common.FprintfIfNotEmpty(w, "Size\t: %d\n", secret.SecretSize)

created := time.Unix(int64(secret.Meta.CreatedAt.Seconds), int64(secret.Meta.CreatedAt.Nanos))
common.FprintfIfNotEmpty(w, "Created\t: %s\n", created.Format(time.RFC822))
common.FprintfIfNotEmpty(w, "Created\t: %s\n", ptypes.TimestampString(secret.Meta.CreatedAt))
}

var (
Expand Down
19 changes: 14 additions & 5 deletions cmd/swarmctl/secrets/list.go → cmd/swarmctl/secret/list.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package secrets
package secret

import (
"errors"
"fmt"
"os"
"sort"
"text/tabwriter"
"time"

"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/cmd/swarmctl/common"
"github.com/docker/swarmkit/protobuf/ptypes"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)
Expand All @@ -19,8 +19,14 @@ type secretSorter []*api.Secret
func (k secretSorter) Len() int { return len(k) }
func (k secretSorter) Swap(i, j int) { k[i], k[j] = k[j], k[i] }
func (k secretSorter) Less(i, j int) bool {
iTime := time.Unix(k[i].Meta.CreatedAt.Seconds, int64(k[i].Meta.CreatedAt.Nanos))
jTime := time.Unix(k[j].Meta.CreatedAt.Seconds, int64(k[j].Meta.CreatedAt.Nanos))
iTime, err := ptypes.Timestamp(k[i].Meta.CreatedAt)
if err != nil {
panic(err)
}
jTime, err := ptypes.Timestamp(k[j].Meta.CreatedAt)
if err != nil {
panic(err)
}
return jTime.Before(iTime)
}

Expand Down Expand Up @@ -59,7 +65,10 @@ var (
}()
common.PrintHeader(w, "ID", "Name", "Created", "Digest", "Size")
output = func(s *api.Secret) {
created := time.Unix(int64(s.Meta.CreatedAt.Seconds), int64(s.Meta.CreatedAt.Nanos))
created, err := ptypes.Timestamp(s.Meta.CreatedAt)
if err != nil {
panic(err)
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n",
s.ID,
s.Spec.Annotations.Name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package secrets
package secret

import (
"errors"
Expand Down
4 changes: 2 additions & 2 deletions manager/controlapi/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// MaxSecretSize is the maximum byte length of the `Secret.Spec.Data` field.
const MaxSecretSize = 500 * 1024 // 500KB

var isValidSecretName = regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9-_.]{0,62}[a-zA-Z0-9])*$`)
var validSecretNameRegexp = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[a-zA-Z0-9-_.]*[a-zA-Z0-9])?$`)

// assumes spec is not nil
func secretFromSecretSpec(spec *api.SecretSpec) *api.Secret {
Expand Down Expand Up @@ -177,7 +177,7 @@ func validateSecretSpec(spec *api.SecretSpec) error {
func validateSecretAnnotations(m api.Annotations) error {
if m.Name == "" {
return grpc.Errorf(codes.InvalidArgument, "name must be provided")
} else if !isValidSecretName.MatchString(m.Name) {
} else if len(m.Name) > 64 || !validSecretNameRegexp.MatchString(m.Name) {
// if the name doesn't match the regex
return grpc.Errorf(codes.InvalidArgument,
"invalid name, only 64 [a-zA-Z0-9-_.] characters allowed, and the start and end character must be [a-zA-Z0-9]")
Expand Down
11 changes: 6 additions & 5 deletions manager/controlapi/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
"testing"

"github.com/docker/swarmkit/api"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestValidateSecretSpec(t *testing.T) {
"with:colon",
"with;semicolon",
"snowman☃",
"o_______________________________________________________________o", // exactly 65 characters
strings.Repeat("a", 65),
} {
err := validateSecretSpec(createSecretSpec(badName, []byte("valid secret"), nil))
assert.Error(t, err)
Expand All @@ -77,12 +78,12 @@ func TestValidateSecretSpec(t *testing.T) {
"0",
"a",
"A",
"name-with-dashes",
"name.with.dots",
"name_with_underscores",
"name-with--dashes",
"name.with..dots",
"name_with__underscores",
"name.with-all_special",
"02624name035with1699numbers015125",
"o______________________________________________________________o", // exactly 64 characters
strings.Repeat("a", 64),
} {
err := validateSecretSpec(createSecretSpec(goodName, []byte("valid secret"), nil))
assert.NoError(t, err)
Expand Down

0 comments on commit 8777042

Please sign in to comment.