2021-05-06 06:02:42 +00:00
|
|
|
package mgmt
|
|
|
|
|
2021-05-18 04:07:25 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/smallstep/certificates/authority/admin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AdminType specifies the type of the admin. e.g. SUPER_ADMIN, REGULAR
|
|
|
|
type AdminType string
|
|
|
|
|
|
|
|
var (
|
|
|
|
// AdminTypeSuper superadmin
|
|
|
|
AdminTypeSuper = AdminType("SUPER_ADMIN")
|
|
|
|
// AdminTypeRegular regular
|
|
|
|
AdminTypeRegular = AdminType("REGULAR")
|
|
|
|
)
|
2021-05-06 06:02:42 +00:00
|
|
|
|
|
|
|
// Admin type.
|
|
|
|
type Admin struct {
|
2021-05-18 04:07:25 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
AuthorityID string `json:"-"`
|
|
|
|
ProvisionerID string `json:"provisionerID"`
|
|
|
|
Subject string `json:"subject"`
|
|
|
|
ProvisionerName string `json:"provisionerName"`
|
|
|
|
ProvisionerType string `json:"provisionerType"`
|
|
|
|
Type AdminType `json:"type"`
|
|
|
|
Status StatusType `json:"status"`
|
2021-05-06 06:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAdmin builds and stores an admin type in the DB.
|
2021-05-18 04:07:25 +00:00
|
|
|
func CreateAdmin(ctx context.Context, db DB, provName, sub string, typ AdminType) (*Admin, error) {
|
2021-05-06 06:02:42 +00:00
|
|
|
adm := &Admin{
|
2021-05-18 04:07:25 +00:00
|
|
|
Subject: sub,
|
|
|
|
ProvisionerName: provName,
|
|
|
|
Type: typ,
|
|
|
|
Status: StatusActive,
|
2021-05-06 06:02:42 +00:00
|
|
|
}
|
|
|
|
if err := db.CreateAdmin(ctx, adm); err != nil {
|
|
|
|
return nil, WrapErrorISE(err, "error creating admin")
|
|
|
|
}
|
|
|
|
return adm, nil
|
|
|
|
}
|
2021-05-18 04:07:25 +00:00
|
|
|
|
|
|
|
// ToCertificates converts an Admin to the Admin type expected by the authority.
|
|
|
|
func (adm *Admin) ToCertificates() (*admin.Admin, error) {
|
|
|
|
return &admin.Admin{
|
|
|
|
ID: adm.ID,
|
|
|
|
Subject: adm.Subject,
|
|
|
|
ProvisionerID: adm.ProvisionerID,
|
|
|
|
ProvisionerName: adm.ProvisionerName,
|
|
|
|
ProvisionerType: adm.ProvisionerType,
|
|
|
|
Type: admin.Type(adm.Type),
|
|
|
|
}, nil
|
|
|
|
}
|