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

Set up ory kratos, add identity admin api #1460

Merged
merged 2 commits into from
Sep 22, 2021

Conversation

shuofan
Copy link
Contributor

@shuofan shuofan commented Aug 16, 2021

What type of this PR

feature

What this PR does / why we need it:

the first step in replacing uc component

Specified Reviewers:

/assign @Effet

@erda-bot erda-bot requested a review from Effet August 16, 2021 09:44
@shuofan shuofan added dop devops plaoform feature labels Aug 16, 2021
@codecov
Copy link

codecov bot commented Aug 16, 2021

Codecov Report

Merging #1460 (810578d) into master (c35c350) will increase coverage by 0.11%.
The diff coverage is 17.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1460      +/-   ##
==========================================
+ Coverage   15.49%   15.61%   +0.11%     
==========================================
  Files        1143     1148       +5     
  Lines      109672   109930     +258     
==========================================
+ Hits        16995    17166     +171     
- Misses      91020    91051      +31     
- Partials     1657     1713      +56     
Impacted Files Coverage Δ
apistructs/user.go 0.00% <ø> (ø)
modules/core-services/initialize.go 0.00% <0.00%> (ø)
modules/dop/initialize.go 5.98% <0.00%> (-0.02%) ⬇️
modules/openapi/api/apis/uc/uc_user_create.go 0.00% <0.00%> (ø)
modules/openapi/api/apis/uc/uc_user_freeze.go 0.00% <0.00%> (ø)
...s/openapi/api/apis/uc/uc_user_list_login_method.go 4.68% <0.00%> (-0.32%) ⬇️
modules/openapi/api/apis/uc/uc_user_unfreeze.go 0.00% <0.00%> (ø)
...les/openapi/api/apis/uc/uc_user_update_userinfo.go 0.00% <0.00%> (ø)
modules/openapi/hooks/posthandle/injectuserinfo.go 24.39% <0.00%> (-1.59%) ⬇️
pkg/ucauth/identity.go 0.00% <0.00%> (ø)
... and 23 more

@shuofan shuofan force-pushed the feature/ory-kratos-setup branch from db847bd to ea72ec5 Compare September 7, 2021 13:55
@shuofan shuofan marked this pull request as ready for review September 7, 2021 13:56
@shuofan shuofan force-pushed the feature/ory-kratos-setup branch from ea72ec5 to 6dadb3b Compare September 9, 2021 12:13
Effet
Effet previously requested changes Sep 10, 2021
@@ -86,6 +86,10 @@ func freezeUser(w http.ResponseWriter, r *http.Request) {
}

func handleFreezeUser(userID, operatorID string, token ucauth.OAuthToken) error {
if token.TokenType == ucauth.OryCompatibleClientId {
return ucauth.ChangeUserState(token.AccessToken, userID, "inactive")
Copy link
Member

Choose a reason for hiding this comment

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

magic value is not good: "inactive"

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

// TODO: password oidc
if token.TokenType == ucauth.OryCompatibleClientId {
return &listLoginTypeResult{
RegistryType: []string{"email"},
Copy link
Member

Choose a reason for hiding this comment

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

magic value

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

@@ -87,6 +87,10 @@ func unfreezeUser(w http.ResponseWriter, r *http.Request) {
}

func handleUnfreezeUser(userID, operatorID string, token ucauth.OAuthToken) error {
if token.TokenType == ucauth.OryCompatibleClientId {
return ucauth.ChangeUserState(token.AccessToken, userID, "active")
Copy link
Member

Choose a reason for hiding this comment

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

magic value

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

cnt++
}
}
if cnt >= 10 {
Copy link
Member

Choose a reason for hiding this comment

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

why only 10 matched users?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

if start > len(i) {
return nil
}
end := start + pageSize
Copy link
Member

Choose a reason for hiding this comment

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

it's better to ensure pageSize > 0

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

@@ -0,0 +1,52 @@
{
"$id": "https://schemas.ory.sh/presets/kratos/quickstart/email-password/identity.schema.json",
Copy link
Member

Choose a reason for hiding this comment

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

looks like it's an example value.

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

@@ -0,0 +1,80 @@
version: v0.4.6-alpha.1
Copy link
Member

Choose a reason for hiding this comment

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

version is too low?

base_url: http://kratos:4434/

selfservice:
default_browser_return_url: http://one.erda.local
Copy link
Member

Choose a reason for hiding this comment

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

should be replace to related path, not static domain

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Config source validation has been improved in later ory/kratos version. we are not using it. it can be replaced by env SELFSERVICE_DEFAULT_BROWSER_RETURN_URL .

Mobile: u.Phone,
Email: u.Email,
Enabled: true,
Locked: u.State == "inactive",
Copy link
Member

Choose a reason for hiding this comment

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

magic value

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

@@ -11,7 +11,7 @@ serve:
base_url: http://kratos:4434/

selfservice:
default_browser_return_url: /
default_browser_return_url: http://one.erda.local
Copy link
Member

Choose a reason for hiding this comment

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

do not user hard-coded domain

@Effet Effet marked this pull request as draft September 11, 2021 14:42
@shuofan shuofan force-pushed the feature/ory-kratos-setup branch from 6dadb3b to 1f5f9cb Compare September 18, 2021 07:13
@@ -0,0 +1,7 @@
CREATE TABLE `kratos_uc_userid_mapping` (
`id` varchar(50) NOT NULL COMMENT 'uc userid',
Copy link
Member

Choose a reason for hiding this comment

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

change id to uc_user_id.

add index for both uc_user_id and user_id separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

add user_id index

func (client *DBClient) InsertMapping(userID, uuid, hash string) error {
return client.Transaction(func(tx *gorm.DB) error {
sql := fmt.Sprintf("UPDATE identity_credentials SET config = JSON_SET(config, '$.hashed_password', ?) WHERE identity_id = ?")
if err := client.Exec(sql, hash, uuid).Error; err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

client.Exec should be tx.Exec ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

absolutely

@@ -244,6 +246,12 @@ func WithFileSvc(svc *filesvc.FileService) Option {
}
}

func WithMigrationSvc(svc *migration.Migration) Option {
Copy link
Member

Choose a reason for hiding this comment

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

Bad pkg. Just put it under uc service.

@@ -433,5 +449,6 @@ func (e *Endpoints) Routes() []httpserver.Endpoint {
{Path: "/api/users", Method: http.MethodGet, Handler: e.ListUser},
{Path: "/api/users/current", Method: http.MethodGet, Handler: e.GetCurrentUser},
{Path: "/api/users/actions/search", Method: http.MethodGet, Handler: e.SearchUser},
{Path: "/api/users/userID", Method: http.MethodGet, Handler: e.GetUcUserID},
Copy link
Member

Choose a reason for hiding this comment

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

Bad path. No uppercase.

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

return
}
for i := 0; i < 3; i++ {
if ep.UcSvc().MigrationReady() {
Copy link
Member

Choose a reason for hiding this comment

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

The condition maybe inverse?

if !conf.OryEnabled() {
return
}
for i := 0; i < 3; i++ {
Copy link
Member

Choose a reason for hiding this comment

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

why 3

@@ -0,0 +1,20 @@
package model
Copy link
Member

Choose a reason for hiding this comment

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

filename should be uc_migration

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

}

// TODO change return url
func OryLogoutURL() string {
return "/.ory/kratos/public/self-service/browser/flows/logout"
Copy link
Member

Choose a reason for hiding this comment

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

Is this url correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will remove deprecated configs

@shuofan shuofan force-pushed the feature/ory-kratos-setup branch 2 times, most recently from 20f8a5e to 79bc038 Compare September 22, 2021 09:43
@shuofan shuofan force-pushed the feature/ory-kratos-setup branch from 79bc038 to d3a43be Compare September 22, 2021 10:11
@shuofan shuofan marked this pull request as ready for review September 22, 2021 10:44
@@ -254,6 +262,14 @@ func (e *Endpoints) GetLocale(request *http.Request) *i18n.LocaleResource {
return e.bdl.GetLocaleByRequest(request)
}

func (e *Endpoints) MigrationSvc() *migration.Migration {
Copy link
Member

Choose a reason for hiding this comment

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

split true user service.

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

lifespan: 10m

registration:
lifespan: 10m
ui_url: /uc/auth/registration
ui_url: http://one.erda.local/uc/auth/registration
Copy link
Member

Choose a reason for hiding this comment

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

add comment in code. Not just on conversation.

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

@sfwn
Copy link
Member

sfwn commented Sep 22, 2021

/approve

@erda-bot erda-bot merged commit af0f09f into erda-project:master Sep 22, 2021
@shuofan
Copy link
Contributor Author

shuofan commented Sep 23, 2021

/cherry-pick release/1.3

erda-bot pushed a commit to erda-bot/erda that referenced this pull request Sep 23, 2021
* Set up ory kratos, add identity admin api

* Code refactor, comment in kratos config
erda-bot added a commit that referenced this pull request Sep 23, 2021
* Set up ory kratos, add identity admin api

* Code refactor, comment in kratos config

Co-authored-by: shuofan <fanshuo2015@gmail.com>
@shuofan shuofan deleted the feature/ory-kratos-setup branch September 23, 2021 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

4 participants