Skip to content

Commit

Permalink
fix: better validation of login parameters
Browse files Browse the repository at this point in the history
The order of REST logging and paraemter handling made for a difficult
read. Now, the duration is validated and the login server validated
first. If the username or password must be prompted for, that happens
next. Then the rest log shows the results of the actual login operation.

Additionally, cleaned up the error message for a login host error;
the low-level error was not helpful in most cases and hard to read for
a non-admin user.
  • Loading branch information
tucats committed Dec 14, 2024
1 parent a89d87c commit 7e387bf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
29 changes: 16 additions & 13 deletions app-cli/app/logon.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,20 @@ func Logon(c *cli.Context) error {
r *resty.Response
)

// If present, set the requested expiration time for the token. This must
// be either empty or a valid duration string. If the user specified a duration
// of days, this is automatically converted to hours. The validation of the
// expiration parameter should be done here, before prompting the user for
// any missing information.
expiration, err := validateExpiration(c)
if err != nil {
return err
}

// Do we know where the logon server is? Start with the default from
// the profile, but if it was explicitly set on the command line, use
// the command line item and update the saved profile setting.
url, err := newFunction(c)
url, err := findLogonServer(c)
if err != nil {
return err
}
Expand All @@ -88,14 +98,9 @@ func Logon(c *cli.Context) error {
pass = ui.PromptPassword(i18n.L("password.prompt"))
}

// If present, set the requested expiration time for the token. This must
// be either empty or a valid duration string.
// If the use specified a duration of days, convert that to hours so
// it's a valid duration expression.
expiration, err := validateExpiration(c)
if err != nil {
return err
}
// Lets not log this until we're successfully prompted for missing input
// and validated that the expiration is okay.
ui.Log(ui.RestLogger, "Logon URL is %s", url)

// Turn logon server address and endpoint into full URL.
url = strings.TrimSuffix(url, "/") + defs.ServicesLogonPath
Expand Down Expand Up @@ -228,7 +233,7 @@ func storeLogonToken(r *resty.Response, user string) error {
return errors.New(err)
}

func newFunction(c *cli.Context) (string, error) {
func findLogonServer(c *cli.Context) (string, error) {
var err error

url := settings.Get(defs.LogonServerSetting)
Expand All @@ -244,8 +249,6 @@ func newFunction(c *cli.Context) (string, error) {

if url == "" {
return "", errors.ErrNoLogonServer
} else {
ui.Log(ui.RestLogger, "Logon URL is %s", url)
}

return url, nil
Expand Down Expand Up @@ -332,7 +335,7 @@ func resolveServerName(name string) (string, error) {

err = rest.Exchange(defs.AdminHeartbeatPath, http.MethodGet, nil, nil, defs.LogonAgent)
if err != nil {
err = errors.New(err)
err = errors.ErrUnableToReachHost.Context(name)
}

return normalizedName, err
Expand Down
1 change: 1 addition & 0 deletions errors/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ var ErrTooManyReturnValues = Message("func.return.count")
var ErrTransactionAlreadyActive = Message("tx.active")
var ErrTryCatchMismatch = Message("try.stack")
var ErrTypeMismatch = Message("type.mismatch")
var ErrUnableToReachHost = Message("host.unreachable")
var ErrUndefinedEntrypoint = Message("entry.not.found")
var ErrUnexpectedParameters = Message("cli.subcommand")
var ErrUnexpectedTextAfterCommand = Message("cli.extra")
Expand Down
1 change: 1 addition & 0 deletions i18n/languages/messages_en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function.values=missing return values
general=general error
go.error=Go routine {{name}}, thread {{id}} failed: {{err}}
http=received HTTP
host.unreachable=cannot connect to host
identifier=invalid identifier
identifier.not.found=unknown identifier
immutable.array=cannot change an immutable array
Expand Down
1 change: 0 additions & 1 deletion lib/http/http.ego
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package http

type Request struct {
Expand Down
2 changes: 1 addition & 1 deletion tools/buildver.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5-1155
1.5-1156
2 changes: 1 addition & 1 deletion tools/zipgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func main() {

ratio := float64(buf.Len()) / float64(rawsize) * 100.0

fmt.Printf("Wrote archive to %s, compressed %d to %d bytes (%2.2f%% of original)\n", output, rawsize, buf.Len(), ratio)
fmt.Printf("Generating %s, compressed %d to %d bytes (%2.2f%% of original)\n", output, rawsize, buf.Len(), ratio)
} else {
if log {
fmt.Println("No zip data written, source unchanged")
Expand Down

0 comments on commit 7e387bf

Please sign in to comment.