-
Notifications
You must be signed in to change notification settings - Fork 129
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
Conversation
…h/T500_graceful_restart
…h/T500_graceful_restart
EventCodeErrorCantHashPassword = 555 | ||
EventCodeErrorCantGetAuthData = 556 | ||
EventCodeErrorCantParseAuthData = 557 | ||
EventCodeErrorCantDumpConfig = 558 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
cmd/acra_configui/acra_configui.go
Outdated
} | ||
line := 0 | ||
for _, authString := range strings.Split(string(authDataSting), "\n") { | ||
authItem := strings.Split(authString, ":") |
There was a problem hiding this comment.
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?
cmd/acra_configui/acra_configui.go
Outdated
log.WithError(err).Errorf("line[%v] DecodeString, user: %v", line, authItem[0]) | ||
continue | ||
} | ||
argon2P := strings.Split(authItem[2], ",") |
There was a problem hiding this comment.
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)
cmd/acra_configui/acra_configui.go
Outdated
os.Exit(1) | ||
} | ||
|
||
err = getAuthData() |
There was a problem hiding this comment.
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) ...
cmd/acra_configui/acra_configui.go
Outdated
return err | ||
} | ||
line := 0 | ||
for _, authString := range strings.Split(string(authDataSting), "\n") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\n
-> LineSeparator
?
cmd/acra_configui/acra_configui.go
Outdated
} | ||
line := 0 | ||
for _, authString := range strings.Split(string(authDataSting), "\n") { | ||
authItem := strings.Split(authString, ":") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:
-> PasswordSeparator
?
cmd/acra_genauth/acra_genauth.go
Outdated
if len(password) == 0 { | ||
return errors.New("passwords is empty") | ||
} | ||
salt := cmd.RandomStringBytes(16) |
There was a problem hiding this comment.
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
cmd/acra_genauth/acra_genauth.go
Outdated
return err | ||
} | ||
a := cmd.UserAuth{Salt: salt, Hash: hashBytes, Argon2Params: argon2Params} | ||
hp[name] = a.UserAuthString(":", ",") |
There was a problem hiding this comment.
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.
cmd/acra_genauth/acra_genauth.go
Outdated
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") |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
long!
cmd/acraserver/auth.go
Outdated
return data, nil | ||
} | ||
|
||
return nil, errors.New(fmt.Sprintf("No auth config [%v]", authPath)) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 :)
cmd/acra_configui/acra_configui.go
Outdated
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) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
cmd/acra_genauth/acra_genauth.go
Outdated
lines := strings.Split(string(htpasswdBytes), LineSeparator) | ||
passwords = make(map[string]string) | ||
for index, line := range lines { | ||
line = strings.Trim(line, " ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add " " as constant?
cmd/acra_genauth/acra_genauth.go
Outdated
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always verbose?
cmd/acra_genauth/acra_genauth.go
Outdated
if *o { | ||
n += 1 | ||
if n > 1 { | ||
log.Errorln("Too many options, use one of --set or --remove") |
There was a problem hiding this comment.
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
cmd/acra_genauth/acra_genauth.go
Outdated
} | ||
err := SetPassword(*filePath, *user, *pwd, keyStore) | ||
if err != nil { | ||
log.WithError(err).Errorln("SetPassword") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Can't set password"
cmd/acra_genauth/acra_genauth.go
Outdated
if *remove { | ||
err := RemoveUser(*filePath, *user, keyStore) | ||
if err != nil { | ||
log.WithError(err).Errorln("RemoveUser") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Can't remove user"
cmd/acra_configui/acra_configui.go
Outdated
Errorf("line[%v] argon2 strconv.ParseUint(%v), user: %v", line, "Time", authItem[0]) | ||
continue | ||
} | ||
Memory, err := strconv.ParseUint(argon2P[1], 10, 32) |
There was a problem hiding this comment.
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
cmd/acra_configui/acra_configui.go
Outdated
Errorf("line[%v] argon2 strconv.ParseUint(%v), user: %v", line, "Memory", authItem[0]) | ||
continue | ||
} | ||
Threads, err := strconv.ParseUint(argon2P[2], 10, 8) |
There was a problem hiding this comment.
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
cmd/acra_configui/acra_configui.go
Outdated
Errorf("line[%v] argon2 strconv.ParseUint(%v), user: %v", line, "Threads", authItem[0]) | ||
continue | ||
} | ||
Length, err := strconv.ParseUint(argon2P[3], 10, 32) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
?
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
cmd/acraserver/auth.go
Outdated
@@ -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)) |
There was a problem hiding this comment.
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")
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it
AcraConfigUI Basic Auth