2018-04-13 12:31:46 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +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 (
|
2018-07-28 08:24:07 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2018-08-28 08:13:53 +00:00
|
|
|
func (ent *cephEntity) toCredentials() *credentials {
|
|
|
|
return &credentials{
|
|
|
|
id: ent.Entity[len(cephEntityClientPrefix):],
|
|
|
|
key: ent.Key,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
func getCephUserName(volID volumeID) string {
|
|
|
|
return cephUserPrefix + string(volID)
|
2018-04-13 12:31:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 10:47:16 +00:00
|
|
|
func getSingleCephEntity(args ...string) (*cephEntity, error) {
|
2018-04-13 12:31:46 +00:00
|
|
|
var ents []cephEntity
|
2019-02-14 10:47:16 +00:00
|
|
|
if err := execCommandJSON(&ents, "ceph", args...); err != nil {
|
|
|
|
return nil, err
|
2018-04-13 12:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ents) != 1 {
|
2019-02-14 10:47:16 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-02-14 10:47:16 +00:00
|
|
|
func getCephUser(volOptions *volumeOptions, adminCr *credentials, volID volumeID) (*cephEntity, error) {
|
2019-02-14 13:38:53 +00:00
|
|
|
adminID, userID := genUserIDs(adminCr, volID)
|
|
|
|
|
2019-02-14 10:47:16 +00:00
|
|
|
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,
|
2019-02-14 10:47:16 +00:00
|
|
|
"-c", cephConfigPath,
|
|
|
|
"-f", "json",
|
2019-02-14 13:38:53 +00:00
|
|
|
"auth", "get", userID,
|
2019-02-14 10:47:16 +00:00
|
|
|
)
|
|
|
|
}
|
2018-04-13 12:31:46 +00:00
|
|
|
|
2019-02-14 10:47:16 +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)
|
|
|
|
|
2019-02-14 10:47:16 +00:00
|
|
|
return getSingleCephEntity(
|
|
|
|
"-m", volOptions.Monitors,
|
2019-02-14 13:38:53 +00:00
|
|
|
"-n", adminID,
|
|
|
|
"--key="+adminCr.key,
|
2019-02-14 10:47:16 +00:00
|
|
|
"-c", cephConfigPath,
|
|
|
|
"-f", "json",
|
2019-02-14 13:38:53 +00:00
|
|
|
"auth", "get-or-create", userID,
|
|
|
|
// User capabilities
|
2019-02-14 10:47:16 +00:00
|
|
|
"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)
|
|
|
|
|
2019-02-14 10:47:16 +00:00
|
|
|
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,
|
2019-02-14 10:47:16 +00:00
|
|
|
"-c", cephConfigPath,
|
2019-02-14 13:38:53 +00:00
|
|
|
"auth", "rm", userID,
|
2019-02-14 10:47:16 +00:00
|
|
|
)
|
2018-04-13 12:31:46 +00:00
|
|
|
}
|