2018-03-20 14:54:24 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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"
|
2019-02-13 12:57:16 +00:00
|
|
|
"io/ioutil"
|
2018-03-20 14:54:24 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"text/template"
|
2019-01-28 13:59:16 +00:00
|
|
|
|
2019-02-08 08:21:22 +00:00
|
|
|
"k8s.io/klog"
|
2018-03-20 14:54:24 +00:00
|
|
|
)
|
|
|
|
|
2019-02-13 12:57:16 +00:00
|
|
|
var cephConfig = []byte(`[global]
|
2018-03-20 14:54:24 +00:00
|
|
|
auth_cluster_required = cephx
|
|
|
|
auth_service_required = cephx
|
|
|
|
auth_client_required = cephx
|
2018-03-26 13:01:13 +00:00
|
|
|
|
|
|
|
# Workaround for http://tracker.ceph.com/issues/23446
|
|
|
|
fuse_set_user_groups = false
|
2019-02-13 12:57:16 +00:00
|
|
|
`)
|
2018-03-20 14:54:24 +00:00
|
|
|
|
2019-01-25 08:40:32 +00:00
|
|
|
const cephKeyring = `[client.{{.UserID}}]
|
2018-03-20 14:54:24 +00:00
|
|
|
key = {{.Key}}
|
|
|
|
`
|
|
|
|
|
2019-01-28 19:55:10 +00:00
|
|
|
const cephSecret = `{{.Key}}` // #nosec
|
2018-03-22 13:01:10 +00:00
|
|
|
|
2018-03-20 14:54:24 +00:00
|
|
|
const (
|
|
|
|
cephConfigRoot = "/etc/ceph"
|
2019-02-13 12:57:16 +00:00
|
|
|
cephConfigPath = "/etc/ceph/ceph.conf"
|
2018-06-12 15:02:14 +00:00
|
|
|
cephKeyringFileNameFmt = "ceph.share.%s.client.%s.keyring"
|
2019-01-28 19:55:10 +00:00
|
|
|
cephSecretFileNameFmt = "ceph.share.%s.client.%s.secret" // #nosec
|
2018-03-20 14:54:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-07-28 08:24:07 +00:00
|
|
|
cephKeyringTempl *template.Template
|
|
|
|
cephSecretTempl *template.Template
|
2018-03-20 14:54:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
fm := map[string]interface{}{
|
|
|
|
"perms": func(readOnly bool) string {
|
|
|
|
if readOnly {
|
|
|
|
return "r"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "rw"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cephKeyringTempl = template.Must(template.New("keyring").Funcs(fm).Parse(cephKeyring))
|
2018-03-22 13:01:10 +00:00
|
|
|
cephSecretTempl = template.Must(template.New("secret").Parse(cephSecret))
|
2018-03-20 14:54:24 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 12:57:16 +00:00
|
|
|
func createCephConfigRoot() error {
|
|
|
|
return os.MkdirAll(cephConfigRoot, 0755) // #nosec
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeCephConfig() error {
|
|
|
|
if err := createCephConfigRoot(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(cephConfigPath, cephConfig, 0640)
|
2018-03-20 14:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeCephTemplate(fileName string, m os.FileMode, t *template.Template, data interface{}) error {
|
2019-02-13 12:57:16 +00:00
|
|
|
if err := createCephConfigRoot(); err != nil {
|
2018-03-20 14:54:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path.Join(cephConfigRoot, fileName), os.O_CREATE|os.O_EXCL|os.O_WRONLY, m)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:59:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
2019-02-08 08:21:22 +00:00
|
|
|
klog.Errorf("failed to close file %s with error %s", f.Name(), err)
|
2019-01-28 13:59:16 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-03-20 14:54:24 +00:00
|
|
|
|
|
|
|
return t.Execute(f, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
type cephKeyringData struct {
|
2019-01-17 05:46:32 +00:00
|
|
|
UserID, Key string
|
2018-07-28 08:24:07 +00:00
|
|
|
VolumeID volumeID
|
2018-04-13 12:38:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
func (d *cephKeyringData) writeToFile() error {
|
2019-01-17 05:46:32 +00:00
|
|
|
return writeCephTemplate(fmt.Sprintf(cephKeyringFileNameFmt, d.VolumeID, d.UserID), 0600, cephKeyringTempl, d)
|
2018-03-20 14:54:24 +00:00
|
|
|
}
|
2018-03-22 13:01:10 +00:00
|
|
|
|
|
|
|
type cephSecretData struct {
|
2019-01-17 05:46:32 +00:00
|
|
|
UserID, Key string
|
2018-07-28 08:24:07 +00:00
|
|
|
VolumeID volumeID
|
2018-03-22 13:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *cephSecretData) writeToFile() error {
|
2019-01-17 05:46:32 +00:00
|
|
|
return writeCephTemplate(fmt.Sprintf(cephSecretFileNameFmt, d.VolumeID, d.UserID), 0600, cephSecretTempl, d)
|
2018-04-13 12:38:44 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
func getCephSecretPath(volID volumeID, userID string) string {
|
|
|
|
return path.Join(cephConfigRoot, fmt.Sprintf(cephSecretFileNameFmt, volID, userID))
|
2018-04-13 12:38:44 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 05:46:32 +00:00
|
|
|
func getCephKeyringPath(volID volumeID, userID string) string {
|
|
|
|
return path.Join(cephConfigRoot, fmt.Sprintf(cephKeyringFileNameFmt, volID, userID))
|
2018-03-22 13:01:10 +00:00
|
|
|
}
|