Skip to content

Commit 8a063e8

Browse files
committedNov 3, 2023
etcd support enable auth
Signed-off-by: wanghai01 <seanhaizi@163.com>
1 parent cf3d33f commit 8a063e8

File tree

15 files changed

+109
-38
lines changed

15 files changed

+109
-38
lines changed
 

‎conf/mds.conf

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ mds.etcd.retry.times=3
2121
mds.etcd.dlock.timeoutMs=10000
2222
# dlock lease timeout
2323
mds.etcd.dlock.ttlSec=10
24+
# etcd auth options
25+
etcd.auth.enable=false
26+
etcd.auth.username=
27+
etcd.auth.password=
2428

2529
#
2630
# segment分配量统计相关配置

‎conf/snapshot_clone_server.conf

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ etcd.retry.times=3
8686
etcd.dlock.timeoutMs=10000
8787
# dlock lease timeout
8888
etcd.dlock.ttlSec=10
89+
# etcd auth options
90+
etcd.auth.enable=false
91+
etcd.auth.username=
92+
etcd.auth.password=
93+
8994

9095
#
9196
# leader选举相关参数

‎curvefs/conf/mds.conf

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ etcd.dailtimeoutMs=5000
3535
etcd.operation.timeoutMs=5000
3636
# number of times a failed operation can be retried
3737
etcd.retry.times=3
38+
# etcd auth options
39+
etcd.auth.enable=false
40+
etcd.auth.username=
41+
etcd.auth.password=
3842

3943
#
4044
# leader election options

‎curvefs/docker/debian11/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM opencurvedocker/curve-base:debian11
2-
COPY libmemcached.so libmemcached.so.11 libhashkit.so.2 /usr/lib/
2+
COPY libmemcached.so libmemcached.so.11 libhashkit.so.2 libetcdclient.so /usr/lib/
33
COPY curvefs /curvefs
44
RUN mkdir -p /etc/curvefs /core /etc/curve && chmod a+x /entrypoint.sh \
55
&& cp /curvefs/tools/sbin/curvefs_tool /usr/bin \
6+
&& cp /curvefs/etcd/sbin/etcdctl /usr/bin/ \
67
&& cp /curvefs/tools-v2/sbin/curve /usr/bin/

‎curvefs/src/mds/mds.cpp

+21-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ MDS::MDS()
5858
etcdClient_(),
5959
leaderElection_(),
6060
status_(),
61-
etcdEndpoint_() {}
61+
etcdEndpoint_(),
62+
etcdUsername_(),
63+
etcdPassword_() {}
6264

6365
MDS::~MDS() {}
6466

@@ -355,28 +357,41 @@ void MDS::InitEtcdClient() {
355357
<< ", etcd address: " << std::string(etcdConf.Endpoints, etcdConf.len)
356358
<< ", etcdtimeout: " << etcdConf.DialTimeout
357359
<< ", operation timeout: " << etcdTimeout
358-
<< ", etcd retrytimes: " << etcdRetryTimes;
360+
<< ", etcd retrytimes: " << etcdRetryTimes
361+
<< ", etcd auth enable: " << etcdConf.authEnable;
359362

360363
LOG_IF(FATAL, !CheckEtcd()) << "Check etcd failed";
361364

362365
LOG(INFO) << "Init etcd client succeeded, etcd address: "
363366
<< std::string(etcdConf.Endpoints, etcdConf.len)
364367
<< ", etcdtimeout: " << etcdConf.DialTimeout
365368
<< ", operation timeout: " << etcdTimeout
366-
<< ", etcd retrytimes: " << etcdRetryTimes;
369+
<< ", etcd retrytimes: " << etcdRetryTimes
370+
<< ", etcd auth enable: " << etcdConf.authEnable;
367371

368372
etcdClientInited_ = true;
369373
}
370374

371375
void MDS::InitEtcdConf(EtcdConf* etcdConf) {
372376
conf_->GetValueFatalIfFail("etcd.endpoint", &etcdEndpoint_);
377+
etcdConf->len = etcdEndpoint_.size();
378+
etcdConf->Endpoints = &etcdEndpoint_[0];
373379
conf_->GetValueFatalIfFail("etcd.dailtimeoutMs", &etcdConf->DialTimeout);
380+
// etcd auth config
381+
bool authEnable = false;
382+
conf_->GetBoolValue("etcd.auth.enable", &authEnable);
383+
etcdConf->authEnable = authEnable ? 1 : 0;
384+
if (authEnable) {
385+
conf_->GetValueFatalIfFail("etcd.auth.username", &etcdUsername_);
386+
etcdConf->username = &etcdUsername_[0];
387+
etcdConf->usernameLen = etcdUsername_.size();
388+
conf_->GetValueFatalIfFail("etcd.auth.password", &etcdPassword_);
389+
etcdConf->password = &etcdPassword_[0];
390+
etcdConf->passwordLen = etcdPassword_.size();
391+
}
374392

375393
LOG(INFO) << "etcd.endpoint: " << etcdEndpoint_;
376394
LOG(INFO) << "etcd.dailtimeoutMs: " << etcdConf->DialTimeout;
377-
378-
etcdConf->len = etcdEndpoint_.size();
379-
etcdConf->Endpoints = &etcdEndpoint_[0];
380395
}
381396

382397
bool MDS::CheckEtcd() {

‎curvefs/src/mds/mds.h

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ class MDS {
181181
bvar::Status<std::string> status_;
182182

183183
std::string etcdEndpoint_;
184+
std::string etcdUsername_;
185+
std::string etcdPassword_;
184186
};
185187

186188
} // namespace mds

‎docker/debian11/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ RUN cd /curve-tgt/curve-sdk && \
1313
make install-programs && \
1414
rm -rf /curve-tgt
1515
COPY curvebs /curvebs
16+
COPY libetcdclient.so /usr/lib/
1617
RUN mkdir -p /etc/curve /etc/nebd /curve/init.d/ && \
1718
chmod a+x /entrypoint.sh && \
1819
cp /curvebs/nbd/sbin/curve-nbd /usr/bin/ && \
1920
cp /curvebs/tools/sbin/curve_ops_tool /usr/bin/ && \
21+
cp /curvebs/etcd/sbin/etcdctl /usr/bin/ && \
2022
cp /curvebs/tools-v2/sbin/curve /usr/bin/

‎src/chunkserver/chunkserver.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ int ChunkServer::Run(int argc, char** argv) {
109109

110110
// 打印参数
111111
conf.PrintConfig();
112-
conf.ExposeMetric("chunkserver_config");
113112
curve::common::ExposeCurveVersion();
114113

115114
// ============================初始化各模块==========================//

‎src/mds/server/mds.cpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ using ::curve::common::BLOCKSIZEKEY;
4545
using ::curve::common::CHUNKSIZEKEY;
4646

4747
MDS::~MDS() {
48-
if (etcdEndpoints_) {
49-
delete etcdEndpoints_;
50-
}
5148
if (fileLockManager_) {
5249
delete fileLockManager_;
5350
}
@@ -202,14 +199,23 @@ void MDS::Stop() {
202199
}
203200

204201
void MDS::InitEtcdConf(EtcdConf* etcdConf) {
205-
std::string endpoint;
206-
conf_->GetValueFatalIfFail("mds.etcd.endpoint", &endpoint);
207-
etcdEndpoints_ = new char[endpoint.size()];
208-
etcdConf->Endpoints = etcdEndpoints_;
209-
std::memcpy(etcdConf->Endpoints, endpoint.c_str(), endpoint.size());
210-
etcdConf->len = endpoint.size();
202+
conf_->GetValueFatalIfFail("mds.etcd.endpoint", &etcdEndpoints_);
203+
etcdConf->len = etcdEndpoints_.size();
204+
etcdConf->Endpoints = &etcdEndpoints_[0];
211205
conf_->GetValueFatalIfFail(
212206
"mds.etcd.dailtimeoutMs", &etcdConf->DialTimeout);
207+
// etcd auth config
208+
bool authEnable = false;
209+
conf_->GetBoolValue("etcd.auth.enable", &authEnable);
210+
etcdConf->authEnable = authEnable ? 1 : 0;
211+
if (authEnable) {
212+
conf_->GetValueFatalIfFail("etcd.auth.username", &etcdUsername_);
213+
etcdConf->username = &etcdUsername_[0];
214+
etcdConf->usernameLen = etcdUsername_.size();
215+
conf_->GetValueFatalIfFail("etcd.auth.password", &etcdPassword_);
216+
etcdConf->password = &etcdPassword_[0];
217+
etcdConf->passwordLen = etcdPassword_.size();
218+
}
213219
}
214220

215221
void MDS::StartServer() {
@@ -261,7 +267,8 @@ void MDS::InitEtcdClient(const EtcdConf& etcdConf,
261267
<< ", etcdaddr len: " << etcdConf.len
262268
<< ", etcdtimeout: " << etcdConf.DialTimeout
263269
<< ", operation timeout: " << etcdTimeout
264-
<< ", etcd retrytimes: " << retryTimes;
270+
<< ", etcd retrytimes: " << retryTimes
271+
<< ", auth enable = " << etcdConf.authEnable;
265272

266273

267274
std::string out;
@@ -275,7 +282,8 @@ void MDS::InitEtcdClient(const EtcdConf& etcdConf,
275282
<< ", etcdaddr len: " << etcdConf.len
276283
<< ", etcdtimeout: " << etcdConf.DialTimeout
277284
<< ", operation timeout: " << etcdTimeout
278-
<< ", etcd retrytimes: " << retryTimes;
285+
<< ", etcd retrytimes: " << retryTimes
286+
<< ", auth enable = " << etcdConf.authEnable;
279287
}
280288

281289
void MDS::InitLeaderElection(const LeaderElectionOptions& leaderElectionOp) {

‎src/mds/server/mds.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,11 @@ class MDS {
235235
std::shared_ptr<CleanDiscardSegmentTask> cleanDiscardSegmentTask_;
236236
std::shared_ptr<Coordinator> coordinator_;
237237
std::shared_ptr<HeartbeatManager> heartbeatManager_;
238-
char* etcdEndpoints_;
239238
FileLockManager* fileLockManager_;
240239
std::shared_ptr<SnapshotCloneClient> snapshotCloneClient_;
240+
std::string etcdEndpoints_;
241+
std::string etcdUsername_;
242+
std::string etcdPassword_;
241243
};
242244

243245
bool ParsePoolsetRules(const std::string& str,

‎src/snapshotcloneserver/snapshotclone_server.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,31 @@ void InitSnapshotCloneServerOptions(std::shared_ptr<Configuration> conf,
115115
&(serverOption->dlockOpts.ttlSec));
116116
}
117117

118-
void InitEtcdConf(std::shared_ptr<Configuration> conf, EtcdConf* etcdConf) {
119-
std::string endpoint;
120-
conf->GetValueFatalIfFail("etcd.endpoint", &endpoint);
121-
char* etcdEndpoints_ = new char[endpoint.size()];
122-
etcdConf->Endpoints = etcdEndpoints_;
123-
std::memcpy(etcdConf->Endpoints, endpoint.c_str(), endpoint.size());
124-
etcdConf->len = endpoint.size();
125-
conf->GetValueFatalIfFail("etcd.dailtimeoutMs", &etcdConf->DialTimeout);
118+
void SnapShotCloneServer::InitEtcdConf(EtcdConf* etcdConf) {
119+
conf_->GetValueFatalIfFail("etcd.endpoint", &etcdEndpoints_);
120+
etcdConf->len = etcdEndpoints_.size();
121+
etcdConf->Endpoints = &etcdEndpoints_[0];
122+
conf_->GetValueFatalIfFail(
123+
"etcd.dailtimeoutMs", &etcdConf->DialTimeout);
124+
// etcd auth config
125+
bool authEnable = false;
126+
conf_->GetBoolValue("etcd.auth.enable", &authEnable);
127+
etcdConf->authEnable = authEnable ? 1 : 0;
128+
if (authEnable) {
129+
conf_->GetValueFatalIfFail("etcd.auth.username", &etcdUsername_);
130+
etcdConf->username = &etcdUsername_[0];
131+
etcdConf->usernameLen = etcdUsername_.size();
132+
conf_->GetValueFatalIfFail("etcd.auth.password", &etcdPassword_);
133+
etcdConf->password = &etcdPassword_[0];
134+
etcdConf->passwordLen = etcdPassword_.size();
135+
}
126136
}
127137

128138
void SnapShotCloneServer::InitAllSnapshotCloneOptions(void) {
129139
InitClientOption(conf_, &(snapshotCloneServerOptions_.clientOptions));
130140
InitSnapshotCloneServerOptions(conf_,
131141
&(snapshotCloneServerOptions_.serverOption));
132-
InitEtcdConf(conf_, &(snapshotCloneServerOptions_.etcdConf));
142+
InitEtcdConf(&(snapshotCloneServerOptions_.etcdConf));
133143

134144
conf_->GetValueFatalIfFail("etcd.operation.timeoutMs",
135145
&(snapshotCloneServerOptions_.etcdClientTimeout));

‎src/snapshotcloneserver/snapshotclone_server.h

+6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class SnapShotCloneServer {
122122
void RunUntilQuit(void);
123123

124124
private:
125+
void InitEtcdConf(EtcdConf* etcdConf);
125126
bool InitEtcdClient(void);
126127

127128
private:
@@ -153,7 +154,12 @@ class SnapShotCloneServer {
153154
std::shared_ptr<CloneServiceManager> cloneServiceManager_;
154155
std::shared_ptr<SnapshotCloneServiceImpl> service_;
155156
std::shared_ptr<brpc::Server> server_;
157+
158+
std::string etcdEndpoints_;
159+
std::string etcdUsername_;
160+
std::string etcdPassword_;
156161
};
162+
157163
} // namespace snapshotcloneserver
158164
} // namespace curve
159165

‎src/tools/metric_name.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const char kCurveVersionMetricName[] = "curve_version";
3737

3838
// snapshot clone server metric name
3939
const char kSnapshotCloneConfMetricName[] =
40-
"snapshotcloneserver_config_server_address";
40+
"snapshot_clone_server_config_server_address";
4141
const char kSnapshotCloneStatusMetricName[] = "snapshotcloneserver_status";
4242
const char kSnapshotCloneStatusActive[] = "active";
4343

‎thirdparties/etcdclient/etcdclient.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ struct EtcdConf {
6868
char *Endpoints;
6969
int len;
7070
int DialTimeout;
71+
int authEnable;
72+
char *username;
73+
int usernameLen;
74+
char *password;
75+
int passwordLen;
7176
};
7277
7378
struct Operation {
@@ -199,16 +204,22 @@ func GetErrCode(op string, err error) C.enum_EtcdErrCode {
199204
}
200205

201206
// TODO(lixiaocui): 日志打印看是否需要glog
207+
//
202208
//export NewEtcdClientV3
203209
func NewEtcdClientV3(conf C.struct_EtcdConf) C.enum_EtcdErrCode {
204210
var err error
205-
globalClient, err = clientv3.New(clientv3.Config{
211+
cfg := clientv3.Config{
206212
Endpoints: GetEndpoint(C.GoStringN(conf.Endpoints, conf.len)),
207213
DialTimeout: time.Duration(int(conf.DialTimeout)) * time.Millisecond,
208214
DialOptions: []grpc.DialOption{grpc.WithBlock()},
209215
DialKeepAliveTime: time.Second,
210216
DialKeepAliveTimeout: time.Second,
211-
})
217+
}
218+
if conf.authEnable == 1 {
219+
cfg.Username = C.GoStringN(conf.username, conf.usernameLen)
220+
cfg.Password = C.GoStringN(conf.password, conf.passwordLen)
221+
}
222+
globalClient, err = clientv3.New(cfg)
212223
return GetErrCode(EtcdNewClient, err)
213224
}
214225

@@ -272,6 +283,7 @@ func EtcdClientGet(timeout C.int, key *C.char,
272283
}
273284

274285
// TODO(lixiaocui): list可能需要有长度限制
286+
//
275287
//export EtcdClientList
276288
func EtcdClientList(timeout C.int, startKey, endKey *C.char,
277289
startLen, endLen C.int) (C.enum_EtcdErrCode, uint64, int64) {
@@ -653,12 +665,12 @@ func EtcdMutexLock(timeout C.int, id C.int64_t) C.enum_EtcdErrCode {
653665

654666
//export EtcdMutexUnlock
655667
func EtcdMutexUnlock(timeout C.int, id C.int64_t) C.enum_EtcdErrCode {
656-
ctx, cancel := context.WithTimeout(context.Background(),
657-
time.Duration(int(timeout))*time.Millisecond)
658-
defer cancel()
668+
ctx, cancel := context.WithTimeout(context.Background(),
669+
time.Duration(int(timeout))*time.Millisecond)
670+
defer cancel()
659671

660-
err := etcdMutex[clientv3.LeaseID(id)].Unlock(ctx)
661-
return GetErrCode(EtcdUnlock, err)
672+
err := etcdMutex[clientv3.LeaseID(id)].Unlock(ctx)
673+
return GetErrCode(EtcdUnlock, err)
662674
}
663675

664676
//export DestoryEtcdMutex

‎util/install.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,11 @@ main() {
338338
elif [ "$g_stor" == "bs" ]; then
339339
install_curvebs
340340
install_playground
341+
install_tools-v2
341342
else
342343
install_curvefs
344+
install_tools-v2
343345
fi
344-
install_tools-v2
345346
}
346347

347348
############################ MAIN()

0 commit comments

Comments
 (0)
Please sign in to comment.