ceph-csi/pkg/cephfs/cephuser.go

109 lines
2.8 KiB
Go
Raw Normal View History

2018-04-13 12:31:46 +00:00
/*
Copyright 2018 The Ceph-CSI Authors.
2018-04-13 12:31:46 +00:00
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 cephfs
import (
"fmt"
)
const (
cephUserPrefix = "user-"
2018-04-13 12:31:46 +00:00
cephEntityClientPrefix = "client."
)
type cephEntityCaps struct {
Mds string `json:"mds"`
Mon string `json:"mon"`
Osd string `json:"osd"`
}
type cephEntity struct {
Entity string `json:"entity"`
Key string `json:"key"`
Caps cephEntityCaps `json:"caps"`
}
func (ent *cephEntity) toCredentials() *credentials {
return &credentials{
id: ent.Entity[len(cephEntityClientPrefix):],
key: ent.Key,
}
}
func getCephUserName(volID volumeID) string {
return cephUserPrefix + string(volID)
2018-04-13 12:31:46 +00:00
}
func getSingleCephEntity(args ...string) (*cephEntity, error) {
2018-04-13 12:31:46 +00:00
var ents []cephEntity
if err := execCommandJSON(&ents, "ceph", args...); err != nil {
return nil, err
2018-04-13 12:31:46 +00:00
}
if len(ents) != 1 {
return nil, fmt.Errorf("got unexpected number of entities: expected 1, got %d", len(ents))
2018-04-13 12:31:46 +00:00
}
return &ents[0], nil
}
2019-02-14 13:38:53 +00:00
func genUserIDs(adminCr *credentials, volID volumeID) (adminID, userID string) {
return cephEntityClientPrefix + adminCr.id, cephEntityClientPrefix + getCephUserName(volID)
}
func getCephUser(volOptions *volumeOptions, adminCr *credentials, volID volumeID) (*cephEntity, error) {
2019-02-14 13:38:53 +00:00
adminID, userID := genUserIDs(adminCr, volID)
return getSingleCephEntity(
2019-02-13 12:57:16 +00:00
"-m", volOptions.Monitors,
2019-02-14 13:38:53 +00:00
"-n", adminID,
"--key="+adminCr.key,
"-c", cephConfigPath,
"-f", "json",
2019-02-14 13:38:53 +00:00
"auth", "get", userID,
)
}
2018-04-13 12:31:46 +00:00
func createCephUser(volOptions *volumeOptions, adminCr *credentials, volID volumeID) (*cephEntity, error) {
2019-02-14 13:38:53 +00:00
adminID, userID := genUserIDs(adminCr, volID)
return getSingleCephEntity(
"-m", volOptions.Monitors,
2019-02-14 13:38:53 +00:00
"-n", adminID,
"--key="+adminCr.key,
"-c", cephConfigPath,
"-f", "json",
2019-02-14 13:38:53 +00:00
"auth", "get-or-create", userID,
// User capabilities
"mds", fmt.Sprintf("allow rw path=%s", getVolumeRootPathCeph(volID)),
"mon", "allow r",
"osd", fmt.Sprintf("allow rw pool=%s namespace=%s", volOptions.Pool, getVolumeNamespace(volID)),
)
2018-04-13 12:31:46 +00:00
}
2019-02-13 12:57:16 +00:00
func deleteCephUser(volOptions *volumeOptions, adminCr *credentials, volID volumeID) error {
2019-02-14 13:38:53 +00:00
adminID, userID := genUserIDs(adminCr, volID)
return execCommandErr("ceph",
2019-02-13 12:57:16 +00:00
"-m", volOptions.Monitors,
2019-02-14 13:38:53 +00:00
"-n", adminID,
"--key="+adminCr.key,
"-c", cephConfigPath,
2019-02-14 13:38:53 +00:00
"auth", "rm", userID,
)
2018-04-13 12:31:46 +00:00
}