-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1437 from openziti/add-debug-admin
Update Changelog/deps and add command to add debug admin
- Loading branch information
Showing
4 changed files
with
152 additions
and
56 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
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,138 @@ | ||
/* | ||
Copyright NetFoundry Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package database | ||
|
||
import ( | ||
"fmt" | ||
"github.com/google/uuid" | ||
"github.com/openziti/storage/boltz" | ||
"github.com/openziti/ziti/common/eid" | ||
"github.com/openziti/ziti/controller/change" | ||
"github.com/openziti/ziti/controller/command" | ||
"github.com/openziti/ziti/controller/db" | ||
"github.com/openziti/ziti/controller/model" | ||
"github.com/openziti/ziti/controller/network" | ||
"github.com/openziti/ziti/controller/persistence" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewAddDebugAdminAction() *cobra.Command { | ||
action := &addDebugAdminAction{} | ||
return &cobra.Command{ | ||
Use: "add-debug-admin </path/to/ziti-controller.db.file> <username> <password>", | ||
Short: "Adds an admin user to the given database file for debugging purposes", | ||
Args: cobra.ExactArgs(3), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
action.run(args[0], args[1], args[2]) | ||
}, | ||
} | ||
} | ||
|
||
type addDebugAdminAction struct { | ||
db boltz.Db | ||
stores *db.Stores | ||
managers *network.Managers | ||
} | ||
|
||
func (action *addDebugAdminAction) GetDb() boltz.Db { | ||
return action.db | ||
} | ||
|
||
func (action *addDebugAdminAction) GetStores() *db.Stores { | ||
return action.stores | ||
} | ||
|
||
func (action *addDebugAdminAction) GetManagers() *network.Managers { | ||
return action.managers | ||
} | ||
|
||
func (action *addDebugAdminAction) noError(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (action *addDebugAdminAction) run(dbFile, username, password string) { | ||
boltDb, err := db.Open(dbFile) | ||
action.noError(err) | ||
|
||
fabricStores, err := db.InitStores(boltDb) | ||
action.noError(err) | ||
|
||
dispatcher := &command.LocalDispatcher{ | ||
EncodeDecodeCommands: false, | ||
} | ||
controllers := network.NewManagers(nil, dispatcher, boltDb, fabricStores, nil) | ||
|
||
dbProvider := &addDebugAdminAction{ | ||
db: boltDb, | ||
stores: fabricStores, | ||
managers: controllers, | ||
} | ||
|
||
stores, err := persistence.NewBoltStores(dbProvider) | ||
action.noError(err) | ||
|
||
id := "debug-admin" | ||
name := fmt.Sprintf("debug admin (%v)", uuid.NewString()) | ||
ctx := change.New().SetChangeAuthorType("cli.debug-db").NewMutateContext() | ||
err = dbProvider.GetDb().Update(ctx, func(ctx boltz.MutateContext) error { | ||
tx := ctx.Tx() | ||
identity, _ := stores.Identity.LoadOneById(tx, id) | ||
if identity != nil { | ||
if err = stores.Identity.DeleteById(ctx, id); err != nil { | ||
return err | ||
} | ||
fmt.Printf("removing existing identity with id '%v'\n", id) | ||
} | ||
|
||
identity = &persistence.Identity{ | ||
BaseExtEntity: boltz.BaseExtEntity{Id: id}, | ||
Name: name, | ||
IdentityTypeId: persistence.DefaultIdentityType, | ||
IsDefaultAdmin: false, | ||
IsAdmin: true, | ||
} | ||
if err = stores.Identity.Create(ctx, identity); err != nil { | ||
return err | ||
} | ||
|
||
authHandler := model.AuthenticatorManager{} | ||
result := authHandler.HashPassword(password) | ||
authenticator := &persistence.AuthenticatorUpdb{ | ||
Authenticator: persistence.Authenticator{ | ||
BaseExtEntity: boltz.BaseExtEntity{ | ||
Id: eid.New(), | ||
}, | ||
Type: "updb", | ||
IdentityId: id, | ||
}, | ||
Username: username, | ||
Password: result.Password, | ||
Salt: result.Salt, | ||
} | ||
authenticator.SubType = authenticator | ||
|
||
if err = stores.Authenticator.Create(ctx, &authenticator.Authenticator); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("added debug admin with username '%v'\n", username) | ||
return nil | ||
}) | ||
action.noError(err) | ||
} |
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