terraform 是一个很不错的基础设施工具,我们可以用来做关于基础设施部署的事情,可以实现基础设施即代码
以下演示一个简单的自签名证书的生成(使用tls provider)

main.tf 文件

 
  1. resource "tls_private_key" "example" {
  1. algorithm = "RSA"
  1. }
  1. resource "tls_self_signed_cert" "example" {
  1. key_algorithm = "${tls_private_key.example.algorithm}"
  1. private_key_pem = "${tls_private_key.example.private_key_pem}"
  1. # Certificate expires after 12 hours.
  1. validity_period_hours = 120000000
  1. # Generate a new certificate if Terraform is run within three
  1. # hours of the certificate's expiration time.
  1. early_renewal_hours = 30000000
  1. is_ca_certificate = true
  1. # Reasonable set of uses for a server SSL certificate.
  1. allowed_uses = [
  1. "key_encipherment",
  1. "digital_signature",
  1. "server_auth",
  1. ]
  1. ip_addresses = ["127.0.0.1","192.168.0.111","10.10.18.119"]
  1. dns_names = ["api.example.com", "k8sapi.example.com"]
  1. subject {
  1. common_name = "example.com"
  1. organization = "example, Inc"
  1. }
  1. }
  1. data "archive_file" "userinfos" {
  1. type = "zip"
  1. output_path = "tf-result/cert.zip"
  1. source {
  1. content = tls_private_key.example.private_key_pem
  1. filename = "private_key_pem"
  1. }
  1. source {
  1. content = tls_private_key.example.public_key_pem
  1. filename = "public_key_pem"
  1. }
  1. source {
  1. content = tls_self_signed_cert.example.cert_pem
  1. filename = "cert_pem"
  1. }
  1. }
 

resource 说明

以上代码使用了archive provider 进行生成文件压缩,使用tls_private_key 生成私钥
使用tls_self_signed_cert 生成自签名证书

运行

  • init 下载插件
 
  1. terraform init
  • 查看计划
  1. terraform plan

效果

  1. Refreshing Terraform state in-memory prior to plan...
  1. The refreshed state will be used to calculate this plan, but will not be
  1. persisted to local or remote state storage.
  1. ------------------------------------------------------------------------
  1. An execution plan has been generated and is shown below.
  1. Resource actions are indicated with the following symbols:
  1. + create
  1. <= read (data resources)
  1. Terraform will perform the following actions:
  1. # data.archive_file.userinfos will be read during apply
  1. # (config refers to values not yet known)
  1. <= data "archive_file" "userinfos" {
  1. + id = (known after apply)
  1. + output_base64sha256 = (known after apply)
  1. + output_md5 = (known after apply)
  1. + output_path = "tf-result/cert.zip"
  1. + output_sha = (known after apply)
  1. + output_size = (known after apply)
  1. + type = "zip"
  1. + source {
  1. + content = (known after apply)
  1. + filename = "cert_pem"
  1. }
  1. + source {
  1. + content = (known after apply)
  1. + filename = "private_key_pem"
  1. }
  1. + source {
  1. + content = (known after apply)
  1. + filename = "public_key_pem"
  1. }
  1. }
  1. # tls_private_key.example will be created
  1. + resource "tls_private_key" "example" {
  1. + algorithm = "RSA"
  1. + ecdsa_curve = "P224"
  1. + id = (known after apply)
  1. + private_key_pem = (known after apply)
  1. + public_key_fingerprint_md5 = (known after apply)
  1. + public_key_openssh = (known after apply)
  1. + public_key_pem = (known after apply)
  1. + rsa_bits = 2048
  1. }
  1. # tls_self_signed_cert.example will be created
  1. + resource "tls_self_signed_cert" "example" {
  1. + allowed_uses = [
  1. + "key_encipherment",
  1. + "digital_signature",
  1. + "server_auth",
  1. ]
  1. + cert_pem = (known after apply)
  1. + dns_names = [
  1. + "api.example.com",
  1. + "k8sapi.example.com",
  1. ]
  1. + early_renewal_hours = 30000000
  1. + id = (known after apply)
  1. + ip_addresses = [
  1. + "127.0.0.1",
  1. + "192.168.0.111",
  1. + "10.10.18.119",
  1. ]
  1. + is_ca_certificate = true
  1. + key_algorithm = "RSA"
  1. + private_key_pem = (known after apply)
  1. + validity_end_time = (known after apply)
  1. + validity_period_hours = 120000000
  1. + validity_start_time = (known after apply)
  1. + subject {
  1. + common_name = "example.com"
  1. + organization = "example, Inc"
  1. }
  1. }
  1. Plan: 2 to add, 0 to change, 0 to destroy.
  1. ------------------------------------------------------------------------
  1. Note: You didn't specify an "-out" parameter to save this plan, so Terraform
  1. can't guarantee that exactly these actions will be performed if
  1. "terraform apply" is subsequently run.
 
 
  • apply
  1. terraform apply

效果

  1. An execution plan has been generated and is shown below.
  1. Resource actions are indicated with the following symbols:
  1. + create
  1. <= read (data resources)
  1. Terraform will perform the following actions:
  1. # data.archive_file.userinfos will be read during apply
  1. # (config refers to values not yet known)
  1. <= data "archive_file" "userinfos" {
  1. + id = (known after apply)
  1. + output_base64sha256 = (known after apply)
  1. + output_md5 = (known after apply)
  1. + output_path = "tf-result/cert.zip"
  1. + output_sha = (known after apply)
  1. + output_size = (known after apply)
  1. + type = "zip"
  1. + source {
  1. + content = (known after apply)
  1. + filename = "cert_pem"
  1. }
  1. + source {
  1. + content = (known after apply)
  1. + filename = "private_key_pem"
  1. }
  1. + source {
  1. + content = (known after apply)
  1. + filename = "public_key_pem"
  1. }
  1. }
  1. # tls_private_key.example will be created
  1. + resource "tls_private_key" "example" {
  1. + algorithm = "RSA"
  1. + ecdsa_curve = "P224"
  1. + id = (known after apply)
  1. + private_key_pem = (known after apply)
  1. + public_key_fingerprint_md5 = (known after apply)
  1. + public_key_openssh = (known after apply)
  1. + public_key_pem = (known after apply)
  1. + rsa_bits = 2048
  1. }
  1. # tls_self_signed_cert.example will be created
  1. + resource "tls_self_signed_cert" "example" {
  1. + allowed_uses = [
  1. + "key_encipherment",
  1. + "digital_signature",
  1. + "server_auth",
  1. ]
  1. + cert_pem = (known after apply)
  1. + dns_names = [
  1. + "api.example.com",
  1. + "k8sapi.example.com",
  1. ]
  1. + early_renewal_hours = 30000000
  1. + id = (known after apply)
  1. + ip_addresses = [
  1. + "127.0.0.1",
  1. + "192.168.0.111",
  1. + "10.10.18.119",
  1. ]
  1. + is_ca_certificate = true
  1. + key_algorithm = "RSA"
  1. + private_key_pem = (known after apply)
  1. + validity_end_time = (known after apply)
  1. + validity_period_hours = 120000000
  1. + validity_start_time = (known after apply)
  1. + subject {
  1. + common_name = "example.com"
  1. + organization = "example, Inc"
  1. }
  1. }
  1. Plan: 2 to add, 0 to change, 0 to destroy.
  1. Do you want to perform these actions?
  1. Terraform will perform the actions described above.
  1. Only 'yes' will be accepted to approve.
  1. Enter a value: yes
  1. tls_private_key.example: Creating...
  1. tls_private_key.example: Creation complete after 0s [id=4bb57b583566785ce23a003432515e07fcebfdba]
  1. tls_self_signed_cert.example: Creating...
  1. tls_self_signed_cert.example: Creation complete after 0s [id=132700825268662052341550768328847386301]
  1. data.archive_file.userinfos: Refreshing state...
  1. Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
 
 
  • 文件内容
  1. unzip cert.zip
  1. Archive: cert.zip
  1. inflating: cert_pem
  1. inflating: private_key_pem
  1. inflating: public_key_pem

说明

我们可以结合vault 的tls 管理以及tf 方便的进行证书管理——基础设施即代码

参考资料

https://www.terraform.io/docs/providers/tls/r/self_signed_cert.html
https://learn.hashicorp.com/vault/secrets-management/sm-pki-engine

使用terraform 生成自签名证书的更多相关文章

  1. cmd命令生成android签名证书

    cmd命令生成android签名证书,有空在写一篇eclipse导出带签名的apk,这里面包括生成新的签名.现在还是讲讲在cmd怎么操作生成签名证书. 1.dos下进入JDK的bin目录 运行如下命令 ...

  2. windows下使用makecert命令生成自签名证书

    1.makecert命令路径 C:\Program Files (x86)\Windows Kits\8.1\bin\x64 2.生成一个自签名证书 makecert -r -pe -n " ...

  3. openssl生成自签名证书

    1.生成x509格式的CA自签名证书 openssl req -new -x509 -keyout ca.key -out ca.crt 2.生成服务端的私钥(key文件)及申请证书文件csr文件 o ...

  4. 用OpenSSL生成自签名证书在IIS上搭建Https站点(用于iOS的https访问)

    前提: 先安装openssl,安装有两种方式,第一种直接下载安装包,装上就可运行:第二种可以自己下载源码,自己编译.这里推荐第一种. 安装包:http://slproweb.com/products/ ...

  5. [ipsec][strongswan] 用strongswan pki工具生成自签名证书

    如题.我在实验环境里,分别要为两个endpoint(T9和T129)生成证书. 证书是如何生成的呢? 证书是由根证书机构签发的.申请证书的人将request提交给根证书机构,然后根证书机构根据requ ...

  6. ios生成自签名证书,实现web下载安装app

    抄自http://beyondvincent.com/blog/2014/03/17/five-tips-for-using-self-signed-ssl-certificates-with-ios ...

  7. 生成自签名证书-开启https

    1.生成CA证书 # 生成 CA 私钥 openssl genrsa -out ca.key 2048 # X.509 Certificate Signing Request (CSR) Manage ...

  8. OpenSSL使用1(用OpenSSL生成自签名证书在IIS上搭建Https站点)(用于iOS的https访问)

    前提: 先安装openssl,安装有两种方式,第一种直接下载安装包,装上就可运行:第二种可以自己下载源码,自己编译.这里推荐第一种. 安装包:http://slproweb.com/products/ ...

  9. Windows下生成自签名证书

    最近通过openssl生成了自签名的证书,总结成下面这张图. 说明:下载openssl0.9.8之后解压,然后运行bin\openssl.exe进入openssl运行环境,然后按上图中顺序执行命令.( ...

随机推荐

  1. Netty源码分析之NioEventLoop(三)—NioEventLoop的执行

    前面两篇文章Netty源码分析之NioEventLoop(一)—NioEventLoop的创建与Netty源码分析之NioEventLoop(二)—NioEventLoop的启动中我们对NioEven ...

  2. 24H玩转 Grafana 被工程师称相当专业,如何做到?

    国庆假期发生了两件小事,其一是我默默度过 35 周岁生日,其二是玩了下grafana `并在节后第一天被工程师 M 称赞:相当专业. 1.我为什么要玩 grafana 呢? 数月前我提交了一份数据后台 ...

  3. ComPtr的介绍以及使用

    ComPtr是为COM而设计的智能指针.它支持WindowsRT,也支持传统Win32.相比ATL里的CComPtr类,它有了一些提升. ComPtr包含在Windows 8.x SDK and Wi ...

  4. SQL Server——死锁查看

    一.通过语句查看 --查询哪些死锁SELECT request_session_id spid, OBJECT_NAME( resource_associated_entity_id ) tableN ...

  5. 在docker容器上如何实现代码的版本管理

    之前在一台centos7的虚拟机上部署了docker并运行了三个容器给开发写代码用,写代码肯定会涉及到版本控制管理. 开始建议是开发在容器中写代码,然后通过docker commit的方式将其保存为i ...

  6. getElementsByClassName兼容 封装

    众所周知,JS获取DOM有个getElementsByClassName,非常方便,但是呢,为了兼容某些浏览器(你懂的).只能 进行封装下了.解决方法如下 <!DOCTYPE html> ...

  7. Java 之 Session

    Session 一.概述 Session技术:服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象(HttpSession)中. 二.使用步骤 1.获取 HttpSession ...

  8. Oracle 逻辑存储结构

    一.总述 逻辑存储结构是 Oracle 数据库存储结构的核心内容,对 Oracle 数据库的所有操作都会涉及逻辑存储结构.逻辑存储结构是从逻辑的角度分析数据库的组成,是对数据存储结构在逻辑概念上的划分 ...

  9. 网络监听工具 嗅探器 SpyNet

    配置网卡 注册 监听配置 开始捕获

  10. unity shader入门(四):高光

    高光反射计算公式(phong模型)Cspecular=(Clight*Mspecular)max(0,v*r)mgloss mgloss为材质的官泽度,也成反射度,控制高光区域亮点有多大 Mspecu ...