使用puppet自动化升级安全程序

    技术2022-07-11  134

    puppet是一种基于ruby语言开发的平台的集中配置管理系统。它使用自有的puppet描述语言,可管理配置文件file、用户user、cron任务、软件包、系统服务等系统实体。通过编写配/etc/puppet/manifests或/etc/puppet/modules下pp格式文件,控制执行同步的内容。puppet agent定时请求puppe master进行同步安装包解压并安装软件。

    我们现有设备都已经安装好puppet环境, 首先在 /etc/hosts文件添加master和angent的ip、hostname信息,ruby为1.8.7版本(puppet对Ruby1.8.7的完美支持),facter为2.3.0版本,hiera为3.3.1版本,puppet为3.7.5版本。

    facter(或者称为fact)用于收集关于主机的信息,来帮助定制Puppet配置。

    hiera是一个强大的puppet外部节点分类器(ENC),利用这个套件,可以大大减少部署中的重复配置。更加灵活高效的配置和管理变量,类,节点配置。

    安装过程参考:https://yq.aliyun.com/articles/43209#,master需要安装puppet-server插件。

    2、配置文件说明

    puppet主要配置文件:

    agnet配置将/puppet-3.7.5/ext/redhat目录下的puppet.conf、fileserver.conf、拷贝到/etc/puppet目录。

    /etc/puppet/puppet.conf为主配置文件主要用于设置相关的参数、认证文件、文件系统配置文件、插件配置等。

    /etc/puppet/fileserver.conf为文件服务器配置文件,主要用于配置同步master安装包存放的路径。

    /etc/puppert/auth.conf为认证配置文件,安装过程中自动生成。

    如需要客户端自动认证,创建/etc/puppet/autosign.conf配置文件,配合puppet.conf使用。

    还需要在/etc/hosts文件添加master和agent的ip、hostname对应信息。

    二、配置

    puppet主要配置文件默认在/etc/puppet/路径如下,举例说明:

     

    agent配置

    agent端需要auth.conf和puppet.conf配置文件,auth.conf采用默认的配置(附录有说明),只需配置puppet.conf主配置文件,其主要配置说明如下:

     

    2、master安装和配置

    安装

    现所有设备puppet都是3.7.5版本,master端需要通过安装包进行安装,解压puppet-3.7.5.tar.gz后进入目录,执行ruby install.rb,执行bin下的./puppet master,即可启动puppet master服务。

    配置

    在启动puppet master之前要进行一系列的配。master端auth.conf采用默认配置即可,主配置文件puppet.conf配置如下:

     

    其中server和ca_server为master的hostname。pluginsync为开启插件同步配置项一般为false。

    fileserver.conf是一项安全配置,结合puppet.conf、auth.conf一起使用。配置puppet客户端允许/禁止访问master的文件目录。通过puppet服务端可以向agent上推送文件 。allow *允许任何通过认证的agent同步,path是同步的源目录,实例配置如下,如果/etc/puppet/modules/genfiles目录不存在则创建。

     

    master的environment.conf配置文件如下:

    manifest = /etc/puppet/manifests

    modulepath = /etc/puppet/modules:/usr/share/puppet/modules

    master配置好执行puppet master启动进程。也可以拷贝cp /puppet-3.7.5/ext/redhat/server.init /etc/init.d/puppetmaster,设置权限chmod u+x /etc/init.d/puppetmaster,执行/etc/init.d/puppetmaster start来启动master进程。

    二、申请认证

    1、首次申请认证

    agent端在第一次连接master端时,会向master端申请证书。如果master端没有授予agent端证书,那么agent端和master端之间的连接是不会建立成功的。

    此时agent端会持续等待master端授权证书,并会每隔2分钟去检查master端是否签发证书。

    agent向master申请证书puppet agent -server $master hostname

    后台启动angent指令为puppet agent server $master hostname daemon。

    agent端申请证书完毕后,切换到master端使用puppet cert命令来对agent端授权证书。puppet cert list查看master端有哪些主机在申请证书。

    给agent端授权证书,使用如下命令:puppet cert sign $certificate name

    如果实际生产环境客户端数量比较多的话,我们可以一次性授权所有证书:puppet cert sign –all

    master执行puppet cert –all查看认证通过的agent以及master的证书。

    可以查看/var/log/message日志文件,查看运行过程中产生的log。

    3、查看认证

    在master端授权完毕后,我们现在切换到agent端查看授权的证书文件,如下:ll /var/lib/puppet/ssl/certs。

     

    master端查看授权的认证执行命令:puppet cert -all。

     

    3、自动认证

    也可以设置成自动授权认证,不提倡使用该方法,存在安全隐患。puppet服务端自动认证修改puppet配置文件实现自动给客户端签名。编辑/etc/puppet/puppet.conf添加如下内容:

    [master]

    autosign=true

    autosign = /etc/puppet/autosign.conf 

    再编辑 /etc/puppet/autosign.conf添加 * 表示所有,或者添加域名,举例: 

    *.oss.letvcdn.com

    4、删除认证

    指定删除:puppet cert --clean $ certificate name

    删除所有:puppet cert --clean -all

    二、puppet代码文件

    master接收到agent的请求后,会查找自己的manifests及相关的配置,master默认情况下manifests入口就是/etc/puppet/manifests/site.pp文件,它会读取这个文件来查找请求主机的节点的定义的类和资源。同时site.pp文件可以inlcude或import其他以pp文件,从而实现针对不同的agent请求,同步不同的资源。

    我们在site.pp文件中定义一些入口配置和全局的配置。

    测试环境中的配置

    pp文件用于控制agent和master同步和操作,当agent向master请求同步时,master解析pp文件里的内容进行和agent的同步和操作,我们可以在该pp文件中指定同步哪些文件,执行哪些指令。

    1、/etc/puppet/modules/nodes/manifests/ad-8000.pp针对agent名为ad-8000的授权访问请求的资源配置文件。

    2、/etc/puppet/modules/common/manifests/init.pp为共有的资源配置文件,可配置成所有agent通用的安装包或安装脚本。

    3、在/etc/puppet/manifests/site.pp全局配置文件中包含:import "../modules/nodes/manifests/ad-8000.pp",ad-8000.pp包含公用的配置模块include common,当名为ad-8000的agent向master请求资源时,就会解析/etc/puppet/modules/nodes/manifests/ad-8000.pp配置文件,进行远程同步。该配置可以解决针对特定agent请求特定配置的同步方案。

    4、也可在在/etc/puppet/manifests/site.pp添加node default{}配置,若agent请求时找不到该agent名的配置模块,就会进入default配置,执行远程同步配置。

     

    例如:以下例子实现了agent和master同步和解压安装操作。

    master端pp入口文件/etc/puppet/manifests/site.pp配置如下:

     

    site.pp包含了CC_install_test.pp文件,文件内容如下:

     

    file指定了同步哪些文件,exec指定了同步文件后执行哪些操作。file首先指定agent同步文件后存放目录。

    source指定了master源文件路径,before指定file里的操作要前于exec操作,先保证文件同步在做解压等操作,避免tar找不到文件。

    exec的onllyif判断条件,用于判断同步的文件存在后再做command操作,timeout设置超时时间0为不设置超时,try_sleep设置命令重试的间隔时间。

     

    四、附录:

    auth.conf认证配置配置文件,参考puppet ACL配置语法。其语法规则如下:

    path [~] {/path/to/resource|regex}  #目录配置  

    [environment {list of environments}]    #环境配置  

    [method {list of methods}]      #方法命令配置  

    [auth[enthicated] {yes|no|on|off|any}]  #授权配置  

    [allow {hostname|certname|*}]       #允许配置

    例如:path /facts  

    auth yes  

    method find, search  

    allow magpie.example.com, dashboard.example.com  

    #意思是允许主机名为"magpie.example.com"和 "dashboard.example.comd的两台客户端在facts目录进行find和search操作

    Processed: 0.010, SQL: 9