Skip to content

Commit dc048d1

Browse files
committed
support enable etcd auth
Signed-off-by: wanghai01 <[email protected]>
1 parent 3b6b0a1 commit dc048d1

File tree

7 files changed

+193
-3
lines changed

7 files changed

+193
-3
lines changed

cli/command/deploy.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const (
4343
CREATE_CONTAINER = playbook.CREATE_CONTAINER
4444
SYNC_CONFIG = playbook.SYNC_CONFIG
4545
START_ETCD = playbook.START_ETCD
46+
ENABLE_ETCD_AUTH = playbook.ENABLE_ETCD_AUTH
4647
START_MDS = playbook.START_MDS
4748
CREATE_PHYSICAL_POOL = playbook.CREATE_PHYSICAL_POOL
4849
START_CHUNKSERVER = playbook.START_CHUNKSERVER
@@ -65,6 +66,7 @@ var (
6566
CREATE_CONTAINER,
6667
SYNC_CONFIG,
6768
START_ETCD,
69+
ENABLE_ETCD_AUTH,
6870
START_MDS,
6971
CREATE_PHYSICAL_POOL,
7072
START_CHUNKSERVER,
@@ -79,13 +81,15 @@ var (
7981
CREATE_CONTAINER,
8082
SYNC_CONFIG,
8183
START_ETCD,
84+
ENABLE_ETCD_AUTH,
8285
START_MDS,
8386
CREATE_LOGICAL_POOL,
8487
START_METASERVER,
8588
}
8689

8790
DEPLOY_FILTER_ROLE = map[int]string{
8891
START_ETCD: ROLE_ETCD,
92+
ENABLE_ETCD_AUTH: ROLE_ETCD,
8993
START_MDS: ROLE_MDS,
9094
START_CHUNKSERVER: ROLE_CHUNKSERVER,
9195
START_SNAPSHOTCLONE: ROLE_SNAPSHOTCLONE,
@@ -99,6 +103,7 @@ var (
99103
CREATE_PHYSICAL_POOL: 1,
100104
CREATE_LOGICAL_POOL: 1,
101105
BALANCE_LEADER: 1,
106+
ENABLE_ETCD_AUTH: 1,
102107
}
103108

104109
CAN_SKIP_ROLES = []string{
@@ -160,11 +165,12 @@ func skipServiceRole(deployConfigs []*topology.DeployConfig, options deployOptio
160165
return dcs
161166
}
162167

163-
func skipDeploySteps(deploySteps []int, options deployOptions) []int {
168+
func skipDeploySteps(dcs []*topology.DeployConfig, deploySteps []int, options deployOptions) []int {
164169
steps := []int{}
165170
skipped := utils.Slice2Map(options.skip)
166171
for _, step := range deploySteps {
167-
if step == START_SNAPSHOTCLONE && skipped[ROLE_SNAPSHOTCLONE] {
172+
if (step == START_SNAPSHOTCLONE && skipped[ROLE_SNAPSHOTCLONE]) ||
173+
(step == ENABLE_ETCD_AUTH && len(dcs) > 0 && !dcs[0].GetEtcdAuthEnable()) {
168174
continue
169175
}
170176
steps = append(steps, step)
@@ -211,7 +217,7 @@ func genDeployPlaybook(curveadm *cli.CurveAdm,
211217
if kind == topology.KIND_CURVEBS {
212218
steps = CURVEBS_DEPLOY_STEPS
213219
}
214-
steps = skipDeploySteps(steps, options)
220+
steps = skipDeploySteps(dcs, steps, options)
215221
poolset := options.poolset
216222
diskType := options.poolsetDiskType
217223

internal/configure/topology/dc_get.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ func (dc *DeployConfig) GetS3Address() string { return dc.getString(CONFI
145145
func (dc *DeployConfig) GetS3BucketName() string { return dc.getString(CONFIG_S3_BUCKET_NAME) }
146146
func (dc *DeployConfig) GetEnableRDMA() bool { return dc.getBool(CONFIG_ENABLE_RDMA) }
147147
func (dc *DeployConfig) GetEnableRenameAt2() bool { return dc.getBool(CONFIG_ENABLE_RENAMEAT2) }
148+
func (dc *DeployConfig) GetEtcdAuthEnable() bool { return dc.getBool(CONFIG_ETCD_AUTH_ENABLE) }
149+
func (dc *DeployConfig) GetEtcdAuthUsername() string { return dc.getString(CONFIG_ETCD_AUTH_USERNAME) }
150+
func (dc *DeployConfig) GetEtcdAuthPassword() string { return dc.getString(CONFIG_ETCD_AUTH_PASSWORD) }
148151
func (dc *DeployConfig) GetEnableChunkfilePool() bool {
149152
return dc.getBool(CONFIG_ENABLE_CHUNKFILE_POOL)
150153
}

internal/configure/topology/dc_item.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,27 @@ var (
285285
true,
286286
nil,
287287
)
288+
289+
CONFIG_ETCD_AUTH_ENABLE = itemset.insert(
290+
"etcd.auth.enable",
291+
REQUIRE_BOOL,
292+
false,
293+
false,
294+
)
295+
296+
CONFIG_ETCD_AUTH_USERNAME = itemset.insert(
297+
"etcd.auth.username",
298+
REQUIRE_STRING,
299+
false,
300+
nil,
301+
)
302+
303+
CONFIG_ETCD_AUTH_PASSWORD = itemset.insert(
304+
"etcd.auth.password",
305+
REQUIRE_STRING,
306+
false,
307+
nil,
308+
)
288309
)
289310

290311
func (i *item) Key() string {

internal/errno/errno.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ var (
417417
ERR_INVALID_DEVICE_USAGE = EC(410020, "invalid device usage")
418418
ERR_ENCRYPT_FILE_FAILED = EC(410021, "encrypt file failed")
419419
ERR_CLIENT_ID_NOT_FOUND = EC(410022, "client id not found")
420+
ERR_ENABLE_ETCD_AUTH_FAILED = EC(410023, "enable etcd auth failed")
420421

421422
// 420: common (curvebs client)
422423
ERR_VOLUME_ALREADY_MAPPED = EC(420000, "volume already mapped")

internal/playbook/factory.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const (
6161
SYNC_CONFIG
6262
START_SERVICE
6363
START_ETCD
64+
ENABLE_ETCD_AUTH
6465
START_MDS
6566
START_CHUNKSERVER
6667
START_SNAPSHOTCLONE
@@ -225,6 +226,8 @@ func (p *Playbook) createTasks(step *PlaybookStep) (*tasks.Tasks, error) {
225226
START_SNAPSHOTCLONE,
226227
START_METASERVER:
227228
t, err = comm.NewStartServiceTask(curveadm, config.GetDC(i))
229+
case ENABLE_ETCD_AUTH:
230+
t, err = comm.NewEnableEtcdAuthTask(curveadm, config.GetDC(i))
228231
case STOP_SERVICE:
229232
t, err = comm.NewStopServiceTask(curveadm, config.GetDC(i))
230233
case RESTART_SERVICE:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2023 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: Curveadm
19+
* Created Date: 2023-08-02
20+
* Author: wanghai (SeanHai)
21+
*/
22+
23+
package scripts
24+
25+
var ENABLE_ETCD_AUTH = `
26+
#!/usr/bin/env bash
27+
28+
if [ $# -ne 3 ]; then
29+
echo "Usage: $0 endpoints username password"
30+
exit 1
31+
fi
32+
33+
endpoints=$1
34+
username=$2
35+
password=$3
36+
root_user=root
37+
38+
# create root user
39+
etcdctl --endpoints=${endpoints} user add ${root_user}:${password} && \
40+
etcdctl --endpoints=${endpoints} user grant-role ${root_user} root || exit 1
41+
42+
# create user if not root
43+
if [ "${username}" != "${root_user}" ]; then
44+
etcdctl --endpoints=${endpoints} user add ${username}:${password} && \
45+
etcdctl --endpoints=${endpoints} user grant-role ${username} root || exit 1
46+
fi
47+
48+
# enable auth
49+
etcdctl --endpoints=${endpoints} auth enable --user=${root_user}:${password} || exit 1
50+
`
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2023 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: Curveadm
19+
* Created Date: 2023-08-02
20+
* Author: wanghai (SeanHai)
21+
*/
22+
23+
package common
24+
25+
import (
26+
"fmt"
27+
28+
"github.com/opencurve/curveadm/cli/cli"
29+
"github.com/opencurve/curveadm/internal/configure/topology"
30+
"github.com/opencurve/curveadm/internal/errno"
31+
"github.com/opencurve/curveadm/internal/task/context"
32+
"github.com/opencurve/curveadm/internal/task/scripts"
33+
"github.com/opencurve/curveadm/internal/task/step"
34+
"github.com/opencurve/curveadm/internal/task/task"
35+
tui "github.com/opencurve/curveadm/internal/tui/common"
36+
)
37+
38+
func checkEnableEtcdAuthStatus(success *bool, out *string) step.LambdaType {
39+
return func(ctx *context.Context) error {
40+
if !*success {
41+
return errno.ERR_ENABLE_ETCD_AUTH_FAILED.S(*out)
42+
}
43+
return nil
44+
}
45+
}
46+
47+
func NewEnableEtcdAuthTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) {
48+
serviceId := curveadm.GetServiceId(dc.GetId())
49+
containerId, err := curveadm.GetContainerId(serviceId)
50+
if curveadm.IsSkip(dc) {
51+
return nil, nil
52+
} else if err != nil {
53+
return nil, err
54+
}
55+
hc, err := curveadm.GetHost(dc.GetHost())
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
var success bool
61+
var out string
62+
host, role := dc.GetHost(), dc.GetRole()
63+
// new task
64+
subname := fmt.Sprintf("host=%s role=%s containerId=%s",
65+
dc.GetHost(), dc.GetRole(), tui.TrimContainerId(containerId))
66+
t := task.NewTask("Enable Etcd Auth", subname, hc.GetSSHConfig())
67+
68+
script := scripts.ENABLE_ETCD_AUTH
69+
layout := dc.GetProjectLayout()
70+
scriptPath := fmt.Sprintf("%s/enable_auth.sh", layout.ServiceBinDir)
71+
72+
etcdEndPoints, err := dc.GetVariables().Get("cluster_etcd_addr")
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
t.AddStep(&step.ListContainers{
78+
ShowAll: true,
79+
Format: `"{{.ID}}"`,
80+
Filter: fmt.Sprintf("id=%s", containerId),
81+
Out: &out,
82+
ExecOptions: curveadm.ExecOptions(),
83+
})
84+
t.AddStep(&step.Lambda{
85+
Lambda: CheckContainerExist(host, role, containerId, &out),
86+
})
87+
t.AddStep(&step.InstallFile{ // install /curvebs(fs)/etcd/sbin/enable_auth.sh
88+
ContainerId: &containerId,
89+
ContainerDestPath: scriptPath,
90+
Content: &script,
91+
ExecOptions: curveadm.ExecOptions(),
92+
})
93+
command := fmt.Sprintf("/bin/bash %s %s %s %s", scriptPath, etcdEndPoints, dc.GetEtcdAuthUsername(),
94+
dc.GetEtcdAuthPassword())
95+
t.AddStep(&step.ContainerExec{
96+
ContainerId: &containerId,
97+
Success: &success,
98+
Out: &out,
99+
Command: command,
100+
ExecOptions: curveadm.ExecOptions(),
101+
})
102+
t.AddStep(&step.Lambda{
103+
Lambda: checkEnableEtcdAuthStatus(&success, &out),
104+
})
105+
return t, nil
106+
}

0 commit comments

Comments
 (0)