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

MF-1008 - Make token duration configurable #1550

Merged
merged 14 commits into from
Jan 25, 2022
Merged

Conversation

rodneyosodo
Copy link
Member

Signed-off-by: 0x6f736f646f blackd0t@protonmail.com

What does this do?

Make token duration configurable at service

Which issue(s) does this PR fix/relate to?

Resolves #1008

List any changes that modify/break current functionality

It makes the token duration configurable

Have you included tests for your changes?

No

Did you document any new/modified functionality?

No

Notes

No

@rodneyosodo rodneyosodo requested a review from a team as a code owner January 20, 2022 14:17
@codecov-commenter
Copy link

codecov-commenter commented Jan 20, 2022

Codecov Report

Merging #1550 (0364c5b) into master (bcc8cf7) will increase coverage by 1.61%.
The diff coverage is 71.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1550      +/-   ##
==========================================
+ Coverage   67.47%   69.09%   +1.61%     
==========================================
  Files         139      134       -5     
  Lines       11321    11058     -263     
==========================================
+ Hits         7639     7640       +1     
+ Misses       3051     2787     -264     
  Partials      631      631              
Impacted Files Coverage Δ
pkg/sdk/go/sdk.go 100.00% <ø> (ø)
provision/api/transport.go 0.00% <0.00%> (ø)
pkg/sdk/go/health.go 46.66% <46.66%> (ø)
auth/service.go 74.39% <100.00%> (+0.12%) ⬆️
bootstrap/api/transport.go 93.10% <100.00%> (ø)
consumers/notifiers/api/transport.go 87.62% <100.00%> (ø)
http/api/transport.go 75.00% <100.00%> (ø)
readers/api/transport.go 78.12% <100.00%> (ø)
things/api/things/http/transport.go 85.30% <100.00%> (ø)
twins/api/http/transport.go 92.48% <100.00%> (ø)
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bcc8cf7...0364c5b. Read the comment docs.

cmd/auth/main.go Outdated
Comment on lines 150 to 155
lduration := mainflux.Env(envLoginDuration, defLoginDuration)
duration, err := strconv.Atoi(lduration)
if err != nil {
log.Fatal(err)
}
loginDuration := time.Duration(duration) * time.Minute
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use

Suggested change
lduration := mainflux.Env(envLoginDuration, defLoginDuration)
duration, err := strconv.Atoi(lduration)
if err != nil {
log.Fatal(err)
}
loginDuration := time.Duration(duration) * time.Minute
loginDuration, err := time.PareDuration(mainflux.Env(envLoginDuration, defLoginDuration))

This also implies that duration is passed using the format with units (1h, 15m, 1500s...) which means that defLoginDuration needs to be changed, and it needs to be updated in README.

Copy link
Member Author

Choose a reason for hiding this comment

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

Initially, the duration is passed in as a string e.g "5" which means 5 minutes. Then we convert this to Integer and finally time.duration

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use time.PareDuration instead and document that the duration unit needs to be passed with the value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

cmd/auth/main.go Outdated
@@ -52,6 +54,7 @@ const (
defKetoHost = "mainflux-keto"
defKetoWritePort = "4467"
defKetoReadPort = "4466"
defLoginDuration = "50"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Make default 10h to be aligned with the old version.

Copy link
Member Author

@rodneyosodo rodneyosodo Jan 21, 2022

Choose a reason for hiding this comment

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

Changed to 600 which is 10h

docker/.env Outdated Show resolved Hide resolved
auth/README.md Outdated
| MF_AUTH_SERVER_KEY | Path to server key in pem format | |
| MF_AUTH_SECRET | String used for signing tokens | auth |
| MF_AUTH_LOGIN_TOKEN_DURATION | Time in minutes for the login token to last of type time.duration | 600m |
| MF_JAEGER_URL | Jaeger server URL | localhost:6831|
Copy link
Contributor

Choose a reason for hiding this comment

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

add space after port value

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

docker/.env Outdated
@@ -35,6 +35,7 @@ MF_AUTH_DB_USER=mainflux
MF_AUTH_DB_PASS=mainflux
MF_AUTH_DB=auth
MF_AUTH_SECRET=secret
MF_AUTH_LOGIN_TOKEN_DURATION="600m"
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe MF_AUTH_LOGIN_TOKEN_EXPIRATION is better naming

Copy link
Member Author

Choose a reason for hiding this comment

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

@dusanb94 had suggested this MF_AUTH_LOGIN_TOKEN_DURATION

auth/README.md Outdated Show resolved Hide resolved
auth/service.go Show resolved Hide resolved
cmd/auth/main.go Outdated Show resolved Hide resolved
auth/README.md Outdated
| MF_AUTH_SERVER_CERT | Path to server certificate in pem format | |
| MF_AUTH_SERVER_KEY | Path to server key in pem format | |
| MF_AUTH_SECRET | String used for signing tokens | auth |
| MF_AUTH_LOGIN_TOKEN_DURATION | Time in hours for the login token to last of type time.duration | 10h |
Copy link
Collaborator

Choose a reason for hiding this comment

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

@0x6f736f646f This is not correct. It is not necessarily in hours, you can change the time unit in the value itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -14,9 +14,6 @@ import (
)

const (
loginDuration = 10 * time.Hour
recoveryDuration = 5 * time.Minute
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't remove recoveryDuration, please keep using the constant for recovery duration.

Copy link
Member Author

Choose a reason for hiding this comment

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

It wasn't being used but I have kept it back

auth/service.go Outdated
@@ -130,9 +131,9 @@ func (svc service) Issue(ctx context.Context, token string, key Key) (Key, strin
case APIKey:
return svc.userKey(ctx, token, key)
case RecoveryKey:
return svc.tmpKey(recoveryDuration, key)
return svc.tmpKey(svc.loginDuration, key)
Copy link
Collaborator

Choose a reason for hiding this comment

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

As I mentioned in this comment, revert this line back to use the constant.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

auth/README.md Outdated
| MF_AUTH_SERVER_CERT | Path to server certificate in pem format | |
| MF_AUTH_SERVER_KEY | Path to server key in pem format | |
| MF_AUTH_SECRET | String used for signing tokens | auth |
| MF_AUTH_LOGIN_TOKEN_DURATION | Time in hours or minutes for the login token to last of type time.duration | 10h |
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is not hours or minutes, it can be seconds (and milliseconds, microseconds; though that's not advisable), as well. Just use:

The duration that represents how long the login token is valid from the moment of its creation

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -142,6 +146,11 @@ func loadConfig() config {
SSLRootCert: mainflux.Env(envDBSSLRootCert, defDBSSLRootCert),
}

loginDuration, err := time.ParseDuration(mainflux.Env(envLoginDuration, defLoginDuration))
Copy link
Contributor

@arvindh123 arvindh123 Jan 24, 2022

Choose a reason for hiding this comment

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

Instead of panic can we use defLoginDuration value
And log the envLoginDuration as warning or error
If the defLoginDuration value got error during parse , then log panic

Like

loginDuration, err := time.ParseDuration(envLoginDuration)
if err != nil {
	logger.Info("Invalid Login Duration %v: %v", envLoginDuration, err)
	loginDuration , err := time.ParseDuration(defLoginDuration)
	if err != nil  {
               log.Painc(fmt.Sprintf("Invalid default login duration %v: %v", defLoginDuration err))
       }
       	logger.Info("Using default Login Duration %v", defLoginDuration)
}	

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's is one way to do it. TBH, since default is not exactly fallback (despite the params naming in mainflux.Env), and due to code simplicity, I prefer the current solution. It's also aligned with the rest of the codebase.

auth/README.md Outdated
| MF_AUTH_SERVER_CERT | Path to server certificate in pem format | |
| MF_AUTH_SERVER_KEY | Path to server key in pem format | |
| MF_AUTH_SECRET | String used for signing tokens | auth |
| MF_AUTH_LOGIN_TOKEN_DURATION | The duration that represents how long the login token is valid from the moment of its created | 10h |
Copy link
Contributor

@manuio manuio Jan 24, 2022

Choose a reason for hiding this comment

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

Maybe you can use something simpler here. The login token lifetime or The login token expiration period

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -37,6 +37,8 @@ const (

authoritiesObj = "authorities"
memberRelation = "member"

Copy link
Contributor

Choose a reason for hiding this comment

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

Kill this blank line

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

dborovcanin
dborovcanin previously approved these changes Jan 25, 2022
auth/service.go Outdated
@@ -14,7 +14,6 @@ import (
)

const (
loginDuration = 10 * time.Hour
recoveryDuration = 5 * time.Minute

Copy link
Contributor

Choose a reason for hiding this comment

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

Kill this blank line

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@manuio
Copy link
Contributor

manuio commented Jan 25, 2022

@arvindh123 can you update your branch please

Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
Copy link
Contributor

@manuio manuio left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

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

LGTM

@drasko drasko merged commit 2abf9da into absmach:master Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make token duration configurable
7 participants