-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
305 additions
and
1,148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package user | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/porter-dev/porter/api/types" | ||
|
||
"github.com/porter-dev/porter/internal/models" | ||
|
||
ory "github.com/ory/client-go" | ||
|
||
"github.com/porter-dev/porter/internal/telemetry" | ||
|
||
"github.com/porter-dev/porter/api/server/handlers" | ||
"github.com/porter-dev/porter/api/server/shared" | ||
"github.com/porter-dev/porter/api/server/shared/apierrors" | ||
"github.com/porter-dev/porter/api/server/shared/config" | ||
) | ||
|
||
// MigrateUsersHandler migrates users into Ory | ||
type MigrateUsersHandler struct { | ||
handlers.PorterHandler | ||
} | ||
|
||
// NewMigrateUsersHandler generates a handler for migrating users | ||
func NewMigrateUsersHandler( | ||
config *config.Config, | ||
decoderValidator shared.RequestDecoderValidator, | ||
writer shared.ResultWriter, | ||
) *MigrateUsersHandler { | ||
return &MigrateUsersHandler{ | ||
PorterHandler: handlers.NewDefaultPorterHandler(config, decoderValidator, writer), | ||
} | ||
} | ||
|
||
// ServeHTTP migrates users into Ory | ||
func (u *MigrateUsersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx, span := telemetry.NewSpan(r.Context(), "serve-migrate-users") | ||
defer span.End() | ||
|
||
r = r.Clone(ctx) | ||
|
||
thisUser, _ := r.Context().Value(types.UserScope).(*models.User) | ||
if !strings.HasSuffix(thisUser.Email, "@porter.run") { | ||
err := telemetry.Error(ctx, span, nil, "user is not a porter user") | ||
u.HandleAPIError(w, r, apierrors.NewErrForbidden(err)) | ||
return | ||
} | ||
|
||
users, err := u.Repo().User().ListUsers() | ||
if err != nil { | ||
err := telemetry.Error(ctx, span, nil, "error listing users") | ||
u.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) | ||
return | ||
} | ||
|
||
var usersMissingAuthMechanism []uint | ||
migrationErrors := map[string][]uint{} | ||
|
||
for _, user := range users { | ||
// skip users that are already migrated | ||
if user.AuthProvider == models.AuthProvider_Ory && user.ExternalId != "" { | ||
continue | ||
} | ||
|
||
createIdentityBody := ory.CreateIdentityBody{ | ||
SchemaId: "preset://email", | ||
Traits: map[string]interface{}{"email": user.Email}, | ||
} | ||
|
||
if user.EmailVerified { | ||
createIdentityBody.VerifiableAddresses = []ory.VerifiableIdentityAddress{ | ||
{ | ||
Value: user.Email, | ||
Verified: true, | ||
Via: "email", | ||
Status: "completed", | ||
}, | ||
} | ||
} | ||
|
||
switch { | ||
case user.Password != "": | ||
password := user.Password | ||
createIdentityBody.Credentials = &ory.IdentityWithCredentials{ | ||
Oidc: nil, | ||
Password: &ory.IdentityWithCredentialsPassword{ | ||
Config: &ory.IdentityWithCredentialsPasswordConfig{ | ||
HashedPassword: &password, | ||
}, | ||
}, | ||
AdditionalProperties: nil, | ||
} | ||
case user.GithubUserID != 0: | ||
createIdentityBody.Credentials = &ory.IdentityWithCredentials{ | ||
Oidc: &ory.IdentityWithCredentialsOidc{ | ||
Config: &ory.IdentityWithCredentialsOidcConfig{ | ||
Config: nil, | ||
Providers: []ory.IdentityWithCredentialsOidcConfigProvider{ | ||
{ | ||
Provider: "github", | ||
Subject: strconv.Itoa(int(user.GithubUserID)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
case user.GoogleUserID != "": | ||
createIdentityBody.Credentials = &ory.IdentityWithCredentials{ | ||
Oidc: &ory.IdentityWithCredentialsOidc{ | ||
Config: &ory.IdentityWithCredentialsOidcConfig{ | ||
Config: nil, | ||
Providers: []ory.IdentityWithCredentialsOidcConfigProvider{ | ||
{ | ||
Provider: "google", | ||
Subject: user.GoogleUserID, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
default: | ||
usersMissingAuthMechanism = append(usersMissingAuthMechanism, user.ID) | ||
continue | ||
} | ||
|
||
createdIdentity, _, err := u.Config().Ory.IdentityAPI.CreateIdentity(u.Config().OryApiKeyContextWrapper(ctx)).CreateIdentityBody(createIdentityBody).Execute() | ||
if err != nil { | ||
errString := fmt.Sprintf("error creating identity: %s", err.Error()) | ||
if len(migrationErrors[err.Error()]) == 0 { | ||
migrationErrors[errString] = []uint{} | ||
} | ||
migrationErrors[errString] = append(migrationErrors[errString], user.ID) | ||
|
||
continue | ||
} | ||
|
||
user.AuthProvider = models.AuthProvider_Ory | ||
user.ExternalId = createdIdentity.Id | ||
|
||
_, err = u.Repo().User().UpdateUser(user) | ||
if err != nil { | ||
errString := fmt.Sprintf("error updating user: %s", err.Error()) | ||
if len(migrationErrors[err.Error()]) == 0 { | ||
migrationErrors[errString] = []uint{} | ||
} | ||
migrationErrors[errString] = append(migrationErrors[errString], user.ID) | ||
continue | ||
} | ||
} | ||
|
||
var errs []error | ||
if len(usersMissingAuthMechanism) > 0 { | ||
errs = append(errs, fmt.Errorf("users missing auth mechanism: %v", usersMissingAuthMechanism)) | ||
} | ||
for errString, userIds := range migrationErrors { | ||
errs = append(errs, fmt.Errorf("%s: %v", errString, userIds)) | ||
} | ||
|
||
if len(errs) > 0 { | ||
err := telemetry.Error(ctx, span, errors.Join(errs...), "error migrating users") | ||
u.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.