etcd入门系列

一. etcd在docker中的安装与使用
二. etcd 开启 https

1. 简介

etcd 默认是没有开启访问控制的,如果我们开启外网访问的话就需要考虑访问控制的问题,etcd 提供了两种访问控制的方式:

  1. 基于身份验证的访问控制
  2. 基于证书的访问控制

这节主要是选择第一种方式,进行的讲解,由于之前文章中是采用http接口的方式通讯,为了更全面的了解 etcd 的使用,本节使用官方提供的 etcdctl 工具进行与服务器之间的通讯。

2. root 用户

rootetcd 的超级管理员,拥有 etcd 的所有权限,在开启角色认证之前为们必须要先建立好 root 用户。还需要注意的是 root 用户必须拥有 root 的角色,允许在 etcd 的所有操作.

3. root 角色

root 角色可以赋予任何用户,拥有 root 角色的用户有全局读写权限和集群身份验证配置权限,此外,还具有修改集群成员身份,碎片整理,建立快照等权限。

4. 用户操作

查看用户列表:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user list

创建用户:

 $ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user add user1

用户可以被赋予角色,也可以被撤销角色:

#赋予权限
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user grant --roles root user1
# 撤销权限
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user revoke --roles root user1

修改用户密码:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user passwd user1

5. 角色操作

角色列表:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role list

创建角色:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role add myrolename
  • 角色没有密码,仅仅是定义的一组访问权限
  • 角色的访问权限可以被赋予read(读),write(写),readwrite(读和写)权限

赋予访问权限范例:

# 给 role1 角色赋予键 /foo 的读操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role grant role1 --path /foo --read
# 给 role1 角色赋予键 /foo 的写操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role grant role1 --path /foo --write
# 给 role1 角色赋予键 /foo 读写操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role grant role1 --path /foo --rw
# 给 role1 角色赋予键 /foo 目录读写操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role grant role1 --path /foo/* --rw

收回访问权限范例:

# 收回 role1 角色对 /foo 的读操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role revoke role1 --path /foo --read
# 收回 role1 角色对 /foo 的写操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role revoke role1 --path /foo --write
# 收回 role1 角色对 /foo 的读写操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role revoke role1 --path /foo --rw
# 收回 role1 角色对 /foo 目录的读写操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role revoke role1 --path /foo/* --rw

查看角色访问权限:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role get role1

删除角色 :

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role remove role1

6. 开启身份验证

1.增加root用户:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user add root
Password of root:

2.开启身份验证:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 etcdctl auth enable

至此,身份验证已经开启,执行下面命令

etcdctl --ca-file /root/cfssl/ca.pem  --endpoints https://192.168.3.3:2379 set /foo bar
//返回bar

这里出现了一个问题,我们已经开启了身份访问验证,却仍然可以不通过任何身份进行操作,这是什么原因呢?其实是因为在 Etcd 开启 Basic Auth 之后,默认会启用两个角色 rootguestrootguest 角色都拥有所有权限,当我们未指定身份的时候其实是通过 guest 角色进行的操作,这里需要注意的是两个角色都不要删除,否则你可能会遇到意想不到的Bug,既然无法删除,那么为们可以通过收回权限的方式对 guest 的权限进行限制,执行下面代码:

etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 ro
le revoke guest --path '/*' --rw
//返回:Role guest updated//查看guest最新的权限信息
etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 ro
le get guest//返回 ://       Role: guest
//        KV Read:
//        KV Write:
// ok 收回权限成功

继续执行之前的set代码 :

etcdctl --ca-file /root/cfssl/ca.pem  --endpoints https://192.168.3.3:2379 set /foo bar
//Error:  110: The request requires user authentication (Insufficient credentials) [0]

如我们所愿的已经不可以对 etcd 进行操作了,下面我们创建一个用户并赋予一个新建的角色试试:

# 创建user2用户
etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 user add user2New password:
//返回 :User user2 created# 创建role2角色
etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 role add role2Role role2 created# 赋予role2 角色权限etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 ro
le grant role2 --path /foo --rw
Role role2 updated# 将用户user2赋予角色role2
etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 us
er  grant --roles role2 user2User user2 updated# 设置 foo 值
etcdctl --ca-file /root/cfssl/ca.pem --username user2:passwod --endpoints https://192.168.3.3:2379:2379 set foo barbar

到此我们想要实现的功能都已实现,个人理解难免有些疏漏和不足,欢迎大神斧正。

7. 参考连接

Role-based access control
Etcd安全配置之Basic Auth认证

原文连接:http://www.artacode.com/posts/etcd/auth/

etcd入门系列三:身份验证访问控制相关推荐

  1. 小猪的C语言快速入门系列(三)

    小猪的C语言快速入门系列(三) 标签: C语言 本节引言: 在上一节中,对C语言的基本语法进行了学习,类比成学英语的话,我们现在 只是会单词而已,组成一个个句子还需要学习一些语法,本节学习的就是两对 ...

  2. 机器学习入门系列三(关键词:逻辑回归,正则化)

    机器学习入门系列三(关键词:逻辑回归,正则化) 目录(?)[+] 一逻辑回归 逻辑回归 假设表示 决策边界 代价函数 其他优化方法 多元分类 二正则化 一.逻辑回归 1.逻辑回归 什么是逻辑回归问题, ...

  3. Reflex WMS入门系列三十二:导出到Excel

    Reflex WMS入门系列三十二:导出到Excel 如同SAP系统的风格 --- 凡是有list的界面,都能导出到Excel ---, Reflex WMS系统也提供了类似的功能.几乎在任何的Lis ...

  4. 零基础数据挖掘入门系列(三) - 数据清洗和转换技巧

    思维导图:零基础入门数据挖掘的学习路径 1. 写在前面 零基础入门数据挖掘是记录自己在Datawhale举办的数据挖掘专题学习中的所学和所想, 该系列笔记使用理论结合实践的方式,整理数据挖掘相关知识, ...

  5. sumo添加车辆_SUMO仿真快速入门系列三:产生车辆移动模型

    在<SUMO快速入门系列二>中,我们已经产生了一个较为简单的街道地图模型. 本节中我们产生车辆移动模型并与道路模型结合,使得车辆在真实道路中跑起来.在SUMO中,车辆移动模型称为Deman ...

  6. SUMO仿真快速入门系列三:产生车辆移动模型

    在<SUMO快速入门系列二>中,我们已经产生了一个较为简单的街道地图模型. 本节中我们产生车辆移动模型并与道路模型结合,使得车辆在真实道路中跑起来.在SUMO中,车辆移动模型称为Deman ...

  7. 保存点云数据_PCL入门系列三——PCL进行数据读写

    本节课我们将了解到以下内容: 基本的PCL中的数据类型: 使用PCL进行简单编程:写文件与读文件. 一.PCL库基本数据类型 上一节课,我们使用PCL库在本地写入了一个名为test_pcd.pcd的文 ...

  8. JNI和NKD入门系列三,在android studio上设置javah和ndk-build的快捷键

    1.首先点开偏好设置 2.点开External Tools并点击添加按钮 3.出来这样的页面,重要的是Insert macro按钮 4.类似的,选择一个内置变量并确定insert 5.会出现一个头尾由 ...

  9. Flask入门系列(转载)

    一.入门系列: Flask入门系列(一)–Hello World 项目开发中,经常要写一些小系统来辅助,比如监控系统,配置系统等等.用传统的Java写,太笨重了,连PHP都嫌麻烦.一直在寻找一个轻量级 ...

最新文章

  1. Spring Cloud构建微服务架构:分布式服务跟踪(整合logstash)【Dalston版】
  2. Git搭建自己的网站服务器(Linux)
  3. python2中的unicode_python2中的unicode()函数在python3中会报错:
  4. 关于CS1061报错(XX不包含XXX的定义,并且找不到类型为XX的第一个参.....)的一种可能的解决的办法...
  5. 用cocos2d-html5做的消除类游戏《英雄爱消除》(1)——系统主菜单
  6. 性能优化篇(4):千万别使用CSS表达式
  7. JavaScript算法(实例九)整数的置换 / 求s=a+aa+aaa+aaaa+aa...a的值 / 自守数
  8. Fragment试手
  9. ASP.NET页面传值方式
  10. Tensorflow源码编译
  11. const 成员函数
  12. 鸿鹄论坛oracle资料,鸿鹄论坛_HCNA-Storage (H13-611)题库 v4.0.pdf
  13. VC++程序设计与应用--MFC应用程序概述
  14. 黑客是如何发现女朋友出轨的
  15. 《Python知识手册》V2.1版,高清PDF免费获取
  16. 运维基本功(六):Linux用户管理与远程管理
  17. 云计算实训报告总结_实训报告心得体会(通用5篇)
  18. IE11下载文件时,文件扩展名自动由点改为下划线
  19. 【转】密钥管理服务(KMS)
  20. Ubuntu14.04系统安装Latex及配置中文字体

热门文章

  1. 解决Win10任务栏图标消失
  2. Android studio 启动模拟器出现 VT-x is disabled in BIOS 以及 /dev/kvm is not found
  3. defer=defer
  4. 登录的双因素认证-Golang实现
  5. Android 使用 ksoap2-android调用Web Service学习
  6. 【Linux操作】常用命令整理(持续更新中...)
  7. 异常检测——5月(task1)
  8. kubernetes cordon原理
  9. 读书笔记——我的学习与研究经历(杨振宁/2012)
  10. erp管理的几个sql