2019-04-22 21:35:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Ceph-CSI 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 util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-08-22 17:19:06 +00:00
|
|
|
"context"
|
2020-06-25 06:41:35 +00:00
|
|
|
"errors"
|
2019-04-22 21:35:39 +00:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2019-06-10 07:22:13 +00:00
|
|
|
|
2020-01-16 15:33:54 +00:00
|
|
|
"github.com/ceph/go-ceph/rados"
|
2020-07-09 05:14:10 +00:00
|
|
|
klog "k8s.io/klog/v2"
|
2019-04-22 21:35:39 +00:00
|
|
|
)
|
|
|
|
|
2020-01-24 16:26:56 +00:00
|
|
|
// InvalidPoolID used to denote an invalid pool
|
|
|
|
const InvalidPoolID int64 = -1
|
|
|
|
|
2019-06-10 07:22:13 +00:00
|
|
|
// ExecCommand executes passed in program with args and returns separate stdout and stderr streams
|
2019-04-22 21:35:39 +00:00
|
|
|
func ExecCommand(program string, args ...string) (stdout, stderr []byte, err error) {
|
|
|
|
var (
|
Address security concerns reported by 'gosec'
gosec reports several issues, none of them looks very critical. With
this change the following concerns have been addressed:
[pkg/cephfs/nodeserver.go:229] - G302: Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM)
> os.Chmod(targetPath, 0777)
[pkg/cephfs/util.go:39] - G204: Subprocess launched with variable (Confidence: HIGH, Severity: MEDIUM)
> exec.Command(program, args...)
[pkg/rbd/nodeserver.go:156] - G302: Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM)
> os.Chmod(stagingTargetPath, 0777)
[pkg/rbd/nodeserver.go:205] - G302: Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM)
> os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0750)
[pkg/rbd/rbd_util.go:797] - G304: Potential file inclusion via variable (Confidence: HIGH, Severity: MEDIUM)
> ioutil.ReadFile(fPath)
[pkg/util/cephcmds.go:35] - G204: Subprocess launched with variable (Confidence: HIGH, Severity: MEDIUM)
> exec.Command(program, args...)
[pkg/util/credentials.go:47] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW)
> os.Remove(tmpfile.Name())
[pkg/util/credentials.go:92] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW)
> os.Remove(cr.KeyFile)
[pkg/util/pidlimit.go:74] - G304: Potential file inclusion via variable (Confidence: HIGH, Severity: MEDIUM)
> os.Open(pidsMax)
URL: https://github.com/securego/gosec
Signed-off-by: Niels de Vos <ndevos@redhat.com>
2019-08-30 10:23:10 +00:00
|
|
|
cmd = exec.Command(program, args...) // nolint: gosec, #nosec
|
2019-06-08 05:06:03 +00:00
|
|
|
sanitizedArgs = StripSecretInArgs(args)
|
|
|
|
stdoutBuf bytes.Buffer
|
|
|
|
stderrBuf bytes.Buffer
|
2019-04-22 21:35:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
cmd.Stderr = &stderrBuf
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return stdoutBuf.Bytes(), stderrBuf.Bytes(), fmt.Errorf("an error (%v)"+
|
2019-06-08 05:06:03 +00:00
|
|
|
" occurred while running %s args: %v", err, program, sanitizedArgs)
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stdoutBuf.Bytes(), nil, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:33:54 +00:00
|
|
|
// GetPoolID fetches the ID of the pool that matches the passed in poolName
|
|
|
|
// parameter
|
2020-05-14 11:23:55 +00:00
|
|
|
func GetPoolID(monitors string, cr *Credentials, poolName string) (int64, error) {
|
2020-01-16 15:33:54 +00:00
|
|
|
conn, err := connPool.Get(monitors, cr.ID, cr.KeyFile)
|
2019-06-28 01:10:32 +00:00
|
|
|
if err != nil {
|
2020-05-14 12:28:55 +00:00
|
|
|
return InvalidPoolID, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-01-16 15:33:54 +00:00
|
|
|
defer connPool.Put(conn)
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2020-01-16 15:33:54 +00:00
|
|
|
id, err := conn.GetPoolByName(poolName)
|
2020-07-02 21:43:40 +00:00
|
|
|
if errors.Is(err, rados.ErrNotFound) {
|
2020-05-14 12:28:55 +00:00
|
|
|
return InvalidPoolID, ErrPoolNotFound{poolName, fmt.Errorf("pool (%s) not found in Ceph cluster", poolName)}
|
2020-01-16 15:33:54 +00:00
|
|
|
} else if err != nil {
|
2020-05-14 12:28:55 +00:00
|
|
|
return InvalidPoolID, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 15:33:54 +00:00
|
|
|
return id, nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 09:24:01 +00:00
|
|
|
// GetPoolName fetches the pool whose pool ID is equal to the requested poolID
|
|
|
|
// parameter
|
2020-05-14 13:28:14 +00:00
|
|
|
func GetPoolName(monitors string, cr *Credentials, poolID int64) (string, error) {
|
2020-03-13 09:24:01 +00:00
|
|
|
conn, err := connPool.Get(monitors, cr.ID, cr.KeyFile)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-03-13 09:24:01 +00:00
|
|
|
defer connPool.Put(conn)
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2020-03-13 09:24:01 +00:00
|
|
|
name, err := conn.GetPoolByID(poolID)
|
|
|
|
if err != nil {
|
|
|
|
return "", ErrPoolNotFound{string(poolID), fmt.Errorf("pool ID (%d) not found in Ceph cluster", poolID)}
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-03-13 09:24:01 +00:00
|
|
|
return name, nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:26:56 +00:00
|
|
|
// GetPoolIDs searches a list of pools in a cluster and returns the IDs of the pools that matches
|
|
|
|
// the passed in pools
|
|
|
|
// TODO this should take in a list and return a map[string(poolname)]int64(poolID)
|
|
|
|
func GetPoolIDs(ctx context.Context, monitors, journalPool, imagePool string, cr *Credentials) (int64, int64, error) {
|
2020-05-14 11:23:55 +00:00
|
|
|
journalPoolID, err := GetPoolID(monitors, cr, journalPool)
|
2020-01-24 16:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return InvalidPoolID, InvalidPoolID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imagePoolID := journalPoolID
|
|
|
|
if imagePool != journalPool {
|
2020-05-14 11:23:55 +00:00
|
|
|
imagePoolID, err = GetPoolID(monitors, cr, imagePool)
|
2020-01-24 16:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return InvalidPoolID, InvalidPoolID, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return journalPoolID, imagePoolID, nil
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
// CreateObject creates the object name passed in and returns ErrObjectExists if the provided object
|
|
|
|
// is already present in rados
|
2019-08-22 17:19:06 +00:00
|
|
|
func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolName, namespace, objectName string) error {
|
2020-06-02 08:03:32 +00:00
|
|
|
conn := ClusterConnection{}
|
|
|
|
err := conn.Connect(monitors, cr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2020-06-02 08:03:32 +00:00
|
|
|
defer conn.Destroy()
|
|
|
|
|
|
|
|
ioctx, err := conn.GetIoctx(poolName)
|
|
|
|
if err != nil {
|
2020-06-25 06:41:35 +00:00
|
|
|
var epnf ErrPoolNotFound
|
|
|
|
if errors.As(err, &epnf) {
|
2020-06-02 08:03:32 +00:00
|
|
|
err = ErrObjectNotFound{poolName, err}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer ioctx.Destroy()
|
2019-05-28 19:03:18 +00:00
|
|
|
|
|
|
|
if namespace != "" {
|
2020-06-02 08:03:32 +00:00
|
|
|
ioctx.SetNamespace(namespace)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 08:03:32 +00:00
|
|
|
err = ioctx.Create(objectName, rados.CreateExclusive)
|
2020-07-02 21:43:40 +00:00
|
|
|
if errors.Is(err, rados.ErrObjectExists) {
|
2020-06-02 08:03:32 +00:00
|
|
|
return ErrObjectExists{objectName, err}
|
|
|
|
} else if err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(Log(ctx, "failed creating omap (%s) in pool (%s): (%v)"), objectName, poolName, err)
|
2019-04-22 21:35:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveObject removes the entire omap name passed in and returns ErrObjectNotFound is provided omap
|
|
|
|
// is not found in rados
|
2019-08-22 17:19:06 +00:00
|
|
|
func RemoveObject(ctx context.Context, monitors string, cr *Credentials, poolName, namespace, oMapName string) error {
|
2020-06-02 08:02:02 +00:00
|
|
|
conn := ClusterConnection{}
|
|
|
|
err := conn.Connect(monitors, cr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2020-06-02 08:02:02 +00:00
|
|
|
defer conn.Destroy()
|
|
|
|
|
|
|
|
ioctx, err := conn.GetIoctx(poolName)
|
|
|
|
if err != nil {
|
2020-06-25 06:41:35 +00:00
|
|
|
var epnf ErrPoolNotFound
|
|
|
|
if errors.As(err, &epnf) {
|
2020-06-02 08:02:02 +00:00
|
|
|
err = ErrObjectNotFound{poolName, err}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer ioctx.Destroy()
|
2019-05-28 19:03:18 +00:00
|
|
|
|
|
|
|
if namespace != "" {
|
2020-06-02 08:02:02 +00:00
|
|
|
ioctx.SetNamespace(namespace)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 08:02:02 +00:00
|
|
|
err = ioctx.Delete(oMapName)
|
2020-07-02 21:43:40 +00:00
|
|
|
if errors.Is(err, rados.ErrNotFound) {
|
2020-06-02 08:02:02 +00:00
|
|
|
return ErrObjectNotFound{oMapName, err}
|
|
|
|
} else if err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Errorf(Log(ctx, "failed removing omap (%s) in pool (%s): (%v)"), oMapName, poolName, err)
|
2019-04-22 21:35:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|