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

Mozh/t489 configui basicauth #141

Merged
merged 37 commits into from
Apr 3, 2018
Merged

Conversation

mozhmike
Copy link
Contributor

@mozhmike mozhmike commented Apr 2, 2018

AcraConfigUI Basic Auth

  • use cmd/acra_genauth.go generate auth user/pwd
  • basic auth is on by default

@vixentael vixentael requested review from Lagovas and vixentael and removed request for Lagovas April 2, 2018 14:51
EventCodeErrorCantHashPassword = 555
EventCodeErrorCantGetAuthData = 556
EventCodeErrorCantParseAuthData = 557
EventCodeErrorCantDumpConfig = 558
Copy link
Collaborator

Choose a reason for hiding this comment

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

after release 0.77 we should change error codes really carefully, because people will use them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I thought exactly the same but solved with min effort)

}
line := 0
for _, authString := range strings.Split(string(authDataSting), "\n") {
authItem := strings.Split(authString, ":")
Copy link
Collaborator

Choose a reason for hiding this comment

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

what about to add function ParseArgon2Params and use it here? to encapsulate complexity of parsing and move it from handling http request?

log.WithError(err).Errorf("line[%v] DecodeString, user: %v", line, authItem[0])
continue
}
argon2P := strings.Split(authItem[2], ",")
Copy link
Collaborator

Choose a reason for hiding this comment

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

what about to use named indexes like

const (
 PARAM1 = iota
 PARAM2 
 PARAM3
 SEPARATOR = ","
 PARAM2_SEPARATOR
)
argon2P := strings.Split(authItem[PARAM2], PARAM2_SEPARATOR)

os.Exit(1)
}

err = getAuthData()
Copy link
Collaborator

Choose a reason for hiding this comment

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

what about getAuthData -> loadAuthData? when I read getAuthData I expected that it will return some value (like getter) ...

return err
}
line := 0
for _, authString := range strings.Split(string(authDataSting), "\n") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

\n -> LineSeparator?

}
line := 0
for _, authString := range strings.Split(string(authDataSting), "\n") {
authItem := strings.Split(authString, ":")
Copy link
Collaborator

Choose a reason for hiding this comment

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

: -> PasswordSeparator?

if len(password) == 0 {
return errors.New("passwords is empty")
}
salt := cmd.RandomStringBytes(16)
Copy link
Collaborator

Choose a reason for hiding this comment

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

better to move 16 to top of file to some constants block with comments

return err
}
a := cmd.UserAuth{Salt: salt, Hash: hashBytes, Argon2Params: argon2Params}
hp[name] = a.UserAuthString(":", ",")
Copy link
Collaborator

Choose a reason for hiding this comment

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

would be more readable if : and , will be constants like DEFAULT_USER_PARAMS_DELIMETER and PARAMS_DELIMETER because I used ctrf+f to understand how this args will be used in UserAuthString and for what.

set := flag.Bool("set", false, "Add/update password for user")
remove := flag.Bool("remove", false, "Remove user")
user := flag.String("user", "", "User")
pwd := flag.String("pwd", "", "Password")
Copy link
Collaborator

Choose a reason for hiding this comment

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

lets use full word as parameter (pwd -> password) if we use full versions of words in other params or use short everywhere (rm|set|usr|pwd|etc)

Copy link
Collaborator

Choose a reason for hiding this comment

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

long!

return data, nil
}

return nil, errors.New(fmt.Sprintf("No auth config [%v]", authPath))
Copy link
Collaborator

Choose a reason for hiding this comment

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

better to create new error variable because then in tests you will need write something like

authData, err := getAuthData("")
if err != errors.New(fmt.Sprintf("No auth config [%v]", authPath)) {
}

to check that error was in case when file not exists

cmd/utils.go Outdated

var randSrc = rand.NewSource(time.Now().UnixNano())

func RandomStringBytes(n int) string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

add some short comment what it do or for what this function (why not rand.Read) because implementation need time to understand :)

Errorf("line[%v] wrong number of argon2 params: got %v, expected %v", line, len(authItem), argon2FieldsCount)
continue
}
Time, err := strconv.ParseUint(argon2P[0], 10, 32)
Copy link
Collaborator

Choose a reason for hiding this comment

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

i can guess that 32 is derived from uint32. what is 10?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it should be ACRA_CONFIGUI_AUTH_ARGON2_TIME

configParamsBytes = []byte(AcraServerCofig)
http.HandleFunc("/index.html", index)
http.HandleFunc("/", index)
http.HandleFunc("/index.html", BasicAuthHandler(index))
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

lines := strings.Split(string(htpasswdBytes), LineSeparator)
passwords = make(map[string]string)
for index, line := range lines {
line = strings.Trim(line, " ")
Copy link
Collaborator

Choose a reason for hiding this comment

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

add " " as constant?

keysDir := flag.String("keys_dir", keystore.DEFAULT_KEY_DIR_SHORT, "Folder from which will be loaded keys")
flag.Parse()
flags := []*bool{set, remove}
logging.SetLogLevel(logging.LOG_VERBOSE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

always verbose?

if *o {
n += 1
if n > 1 {
log.Errorln("Too many options, use one of --set or --remove")
Copy link
Collaborator

Choose a reason for hiding this comment

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

please add error code smth like wrong params to each of the error logs below

}
err := SetPassword(*filePath, *user, *pwd, keyStore)
if err != nil {
log.WithError(err).Errorln("SetPassword")
Copy link
Collaborator

Choose a reason for hiding this comment

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

"Can't set password"

if *remove {
err := RemoveUser(*filePath, *user, keyStore)
if err != nil {
log.WithError(err).Errorln("RemoveUser")
Copy link
Collaborator

Choose a reason for hiding this comment

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

"Can't remove user"

Errorf("line[%v] argon2 strconv.ParseUint(%v), user: %v", line, "Time", authItem[0])
continue
}
Memory, err := strconv.ParseUint(argon2P[1], 10, 32)
Copy link
Collaborator

Choose a reason for hiding this comment

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

32 -> ACRA_CONFIGUI_AUTH_ARGON2_MEMORY

Errorf("line[%v] argon2 strconv.ParseUint(%v), user: %v", line, "Memory", authItem[0])
continue
}
Threads, err := strconv.ParseUint(argon2P[2], 10, 8)
Copy link
Collaborator

Choose a reason for hiding this comment

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

8 -> ACRA_CONFIGUI_AUTH_ARGON2_THREADS

Errorf("line[%v] argon2 strconv.ParseUint(%v), user: %v", line, "Threads", authItem[0])
continue
}
Length, err := strconv.ParseUint(argon2P[3], 10, 32)
Copy link
Collaborator

Choose a reason for hiding this comment

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

32 -> ACRA_CONFIGUI_AUTH_ARGON2_LENGTH

@@ -248,3 +254,78 @@ func Parse(configPath string) error {
}
return nil
}

type Argon2Params struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't we put all these argon-specific utils into separate file? like utils-argon2.go?

Copy link
Collaborator

@Lagovas Lagovas left a comment

Choose a reason for hiding this comment

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

code has become more readable 👍

var netClient = &http.Client{
Timeout: time.Second * HTTP_TIMEOUT,
}
serverResponse, err := netClient.Get(fmt.Sprintf("http://%v:%v/loadAuthData", *acraHost, *acraPort))
Copy link
Collaborator

Choose a reason for hiding this comment

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

https://golang.org/pkg/net/http/ - The client must close the response body when finished with it: defer serverResponse.Body.Close()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -38,6 +38,6 @@ func getAuthData(authPath string) (data []byte, err error) {
data = fileContent
return data, nil
}

return nil, errors.New(fmt.Sprintf("No auth config [%v]", authPath))
err = errors.New(fmt.Sprintf("No auth config [%v]", authPath))
Copy link
Collaborator

Choose a reason for hiding this comment

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

in previous my comment I mean to add error variable in such way:

var ErrGetAuthDataFromFile = errors.New(fmt.Sprintf("No auth config [%v]", authPath)))
func getAuthDataFromFile(authPath string) (data []byte, err error) {
...
return nil, ErrGetAuthDataFromFile

and then we can test:

func TestGetAuthDataFromFile(t *testing.T){
  if _, err := getAuthDataFromFile("some path"); err != ErrGetAuthDataFromFile {
    t.Fatal("expected ErrGetAuthDataFromFile error")
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it

@vixentael vixentael merged commit 945d64e into master Apr 3, 2018
@vixentael vixentael deleted the mozh/T489_configui_basicauth branch April 3, 2018 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants