mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/yunionio/cloudpods.git
synced 2026-09-20 08:03:53 +08:00
163 lines
5.0 KiB
Go
163 lines
5.0 KiB
Go
// Copyright 2019 Yunion
|
|
//
|
|
// 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
|
|
//
|
|
// http://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 shell
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"yunion.io/x/jsonutils"
|
|
|
|
"yunion.io/x/onecloud/pkg/mcclient"
|
|
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
|
)
|
|
|
|
func init() {
|
|
type CredentialListOptions struct {
|
|
Type string `help:"credential type" choices:"totp|recovery|ec2"`
|
|
User string `help:"filter by user"`
|
|
UserDomain string `help:"the domain of user"`
|
|
}
|
|
R(&CredentialListOptions{}, "credential-list", "List all credentials", func(s *mcclient.ClientSession, args *CredentialListOptions) error {
|
|
query := jsonutils.NewDict()
|
|
if len(args.Type) > 0 {
|
|
query.Add(jsonutils.NewString(args.Type), "type")
|
|
}
|
|
var err error
|
|
if len(args.User) > 0 {
|
|
domainId := "default"
|
|
if len(args.UserDomain) > 0 {
|
|
domainId, err = modules.Domains.GetId(s, args.UserDomain, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
userQuery := jsonutils.NewDict()
|
|
userQuery.Add(jsonutils.NewString(domainId), "domain_id")
|
|
userId, err := modules.UsersV3.GetId(s, args.User, userQuery)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
query.Add(jsonutils.NewString(userId), "user_id")
|
|
}
|
|
results, err := modules.Credentials.List(s, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
printList(results, nil)
|
|
return nil
|
|
})
|
|
|
|
type CredentialTOTPOptions struct {
|
|
USER string `help:"User"`
|
|
UserDomain string `help:"domain of user"`
|
|
}
|
|
R(&CredentialTOTPOptions{}, "credential-create-totp", "Create totp credential", func(s *mcclient.ClientSession, args *CredentialTOTPOptions) error {
|
|
uid, err := modules.UsersV3.FetchId(s, args.USER, args.UserDomain)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
secret, err := modules.Credentials.CreateTotpSecret(s, uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println("secret:", secret)
|
|
return nil
|
|
})
|
|
|
|
R(&CredentialTOTPOptions{}, "credential-get-totp", "Get totp credential for user", func(s *mcclient.ClientSession, args *CredentialTOTPOptions) error {
|
|
uid, err := modules.UsersV3.FetchId(s, args.USER, args.UserDomain)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
secret, err := modules.Credentials.GetTotpSecret(s, uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println("secret:", secret)
|
|
return nil
|
|
})
|
|
|
|
R(&CredentialTOTPOptions{}, "credential-remove-totp", "Remove totp credential for user", func(s *mcclient.ClientSession, args *CredentialTOTPOptions) error {
|
|
uid, err := modules.UsersV3.FetchId(s, args.USER, args.UserDomain)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = modules.Credentials.RemoveTotpSecrets(s, uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println("success")
|
|
return nil
|
|
})
|
|
|
|
type CredentialCreateRecoverySecretsOptions struct {
|
|
USER string `help:"User"`
|
|
UserDomain string `help:"domain of user"`
|
|
Question []string `help:"questions"`
|
|
Answer []string `help:"answers"`
|
|
}
|
|
R(&CredentialCreateRecoverySecretsOptions{}, "credential-save-recovery-secrets", "save recovery secrets for user", func(s *mcclient.ClientSession, args *CredentialCreateRecoverySecretsOptions) error {
|
|
uid, err := modules.UsersV3.FetchId(s, args.USER, args.UserDomain)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(args.Question) == 0 || len(args.Answer) == 0 {
|
|
return fmt.Errorf("no recovery secrets provided")
|
|
}
|
|
if len(args.Question) != len(args.Answer) {
|
|
return fmt.Errorf("number of questions and answers does not match")
|
|
}
|
|
secrets := make([]modules.SRecoverySecret, len(args.Question))
|
|
for i := range args.Question {
|
|
secrets[i] = modules.SRecoverySecret{
|
|
Question: args.Question[i],
|
|
Answer: args.Answer[i],
|
|
}
|
|
}
|
|
err = modules.Credentials.SaveRecoverySecrets(s, uid, secrets)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println("success")
|
|
return nil
|
|
})
|
|
|
|
R(&CredentialTOTPOptions{}, "credential-get-recovery-secrets", "Get totp credential for user", func(s *mcclient.ClientSession, args *CredentialTOTPOptions) error {
|
|
uid, err := modules.UsersV3.FetchId(s, args.USER, args.UserDomain)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
secret, err := modules.Credentials.GetRecoverySecrets(s, uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println("secret:", secret)
|
|
return nil
|
|
})
|
|
|
|
R(&CredentialTOTPOptions{}, "credential-remove-recovery-secrets", "Remove totp credential for user", func(s *mcclient.ClientSession, args *CredentialTOTPOptions) error {
|
|
uid, err := modules.UsersV3.FetchId(s, args.USER, args.UserDomain)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = modules.Credentials.RemoveRecoverySecrets(s, uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println("success")
|
|
return nil
|
|
})
|
|
}
|