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

Add -topo_zk_auth_file flag #4733

Merged
merged 2 commits into from
Mar 31, 2019
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
36 changes: 24 additions & 12 deletions go/cmd/zk/zkcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ there are some slight differences in flag handling.

zk -h - provide help on overriding cell selection

zk addAuth digest user:pass

zk cat /zk/path
zk cat -l /zk/path1 /zk/path2 (list filename before file data)

Expand Down Expand Up @@ -111,18 +113,19 @@ var zconn *zk2topo.ZkConn

func init() {
cmdMap = map[string]cmdFunc{
"cat": cmdCat,
"chmod": cmdChmod,
"cp": cmdCp,
"edit": cmdEdit,
"ls": cmdLs,
"rm": cmdRm,
"stat": cmdStat,
"touch": cmdTouch,
"unzip": cmdUnzip,
"wait": cmdWait,
"watch": cmdWatch,
"zip": cmdZip,
"addAuth": cmdAddAuth,
"cat": cmdCat,
"chmod": cmdChmod,
"cp": cmdCp,
"edit": cmdEdit,
"ls": cmdLs,
"rm": cmdRm,
"stat": cmdStat,
"touch": cmdTouch,
"unzip": cmdUnzip,
"wait": cmdWait,
"watch": cmdWatch,
"zip": cmdZip,
}
}

Expand Down Expand Up @@ -500,6 +503,15 @@ func cmdRm(ctx context.Context, subFlags *flag.FlagSet, args []string) error {
return nil
}

func cmdAddAuth(ctx context.Context, subFlags *flag.FlagSet, args []string) error {
subFlags.Parse(args)
if subFlags.NArg() < 2 {
return fmt.Errorf("addAuth: expected args <scheme> <auth>")
}
scheme, auth := subFlags.Arg(0), subFlags.Arg(1)
return zconn.AddAuth(ctx, scheme, []byte(auth))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any assumptions about the args that should be validated here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can only validate if we limit the schemes -- e.g., digest scheme should have format user:pass, ip scheme should have format addr/mask. However, ZooKeeper's auth system is pluggable, so someone could write scheme foobar with some unknown format which we cannot validate.

}

func cmdCat(ctx context.Context, subFlags *flag.FlagSet, args []string) error {
var (
longListing = subFlags.Bool("l", false, "long listing")
Expand Down
33 changes: 33 additions & 0 deletions go/vt/topo/zk2topo/zk_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var (
certPath = flag.String("topo_zk_tls_cert", "", "the cert to use to connect to the zk topo server, requires topo_zk_tls_key, enables TLS")
keyPath = flag.String("topo_zk_tls_key", "", "the key to use to connect to the zk topo server, enables TLS")
caPath = flag.String("topo_zk_tls_ca", "", "the server ca to use to validate servers when connecting to the zk topo server")
authFile = flag.String("topo_zk_auth_file", "", "auth to use when connecting to the zk topo server, file contents should be <scheme>:<auth>, e.g., digest:user:pass")
)

// Time returns a time.Time from a ZK int64 milliseconds since Epoch time.
Expand Down Expand Up @@ -195,6 +196,14 @@ func (c *ZkConn) SetACL(ctx context.Context, path string, aclv []zk.ACL, version
})
}

// AddAuth is part of the Conn interface.
func (c *ZkConn) AddAuth(ctx context.Context, scheme string, auth []byte) error {
return c.withRetry(ctx, func(conn *zk.Conn) error {
err := conn.AddAuth(scheme, auth)
return err
})
}

// Close is part of the Conn interface.
func (c *ZkConn) Close() error {
c.mu.Lock()
Expand Down Expand Up @@ -271,10 +280,34 @@ func (c *ZkConn) getConn(ctx context.Context) (*zk.Conn, error) {
}
c.conn = conn
go c.handleSessionEvents(conn, events)
c.maybeAddAuth(ctx)
}
return c.conn, nil
}

// maybeAddAuth calls AddAuth if the `-topo_zk_auth_file` flag was specified
func (c *ZkConn) maybeAddAuth(ctx context.Context) {
if *authFile == "" {
return
}
authInfoBytes, err := ioutil.ReadFile(*authFile)
if err != nil {
log.Errorf("failed to read topo_zk_auth_file: %v", err)
return
}
authInfo := string(authInfoBytes)
authInfoParts := strings.SplitN(authInfo, ":", 2)
if len(authInfoParts) != 2 {
log.Errorf("failed to parse topo_zk_auth_file contents, expected format <scheme>:<auth> but saw: %s", authInfo)
return
}
err = c.conn.AddAuth(authInfoParts[0], []byte(authInfoParts[1]))
if err != nil {
log.Errorf("failed to add auth from topo_zk_auth_file: %v", err)
return
}
}

// handleSessionEvents is processing events from the session channel.
// When it detects that the connection is not working any more, it
// clears out the connection record.
Expand Down