我正在处理两个文件,一个包含我的Base64证书链,另一个是私钥,我想合并它们,私钥是在第一次出现后添加的

-----END CERTIFICATE-----

.我不知道如何在狂欢中做到这一点.

如果能得到帮助就太好了.谢谢!

Certificate chain file

-----BEGIN CERTIFICATE-----
... (certificate for your server)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the intermediate certificate)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the root certificate for the CA)...
-----END CERTIFICATE----

private key file

-----BEGIN PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: DES-EDE3-CBC,CFCECC7976725DE5
    <Server Private Key – Passphrase protected>
-----END PRIVATE KEY-----

final merged file

-----BEGIN CERTIFICATE-----
... (certificate for your server)...
-----END CERTIFICATE-----
-----BEGIN PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: DES-EDE3-CBC,CFCECC7976725DE5
    <Server Private Key – Passphrase protected>
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
... (the intermediate certificate)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the root certificate for the CA)...
-----END CERTIFICATE----

代码片段脚本:

get_new_certificate() {

    curl --get "$venafi_url$venafi_get_cert_endpoint?Format=Base64&IncludeChain=true&RootFirstOrder=false" --insecure --data-urlencode "CertificateDN=$certificate_identifier" \
    -H "accept: application/json" \
    -H "Authorization: Bearer $auth_token" \
    -o "$certificate_path"

   # Extract the private key block
   #private_key=$( awk '/-----BEGIN PRIVATE KEY-----/,/-----END PRIVATE KEY-----/' "$PRIVATE_KEY_PATH" | tr '\n' '\f')
   private_key=$( cat "$PRIVATE_KEY_PATH")
   # Combine the certificates and private key in the desired order
   #sed -i -z "0,/-----END CERTIFICATE-----/s/ $private_key"  "$certificate_path" |  tr '\f' '\n'
   #awk '/-----END CERTIFICATE-----/ {print; print "'"$private_key"'"; next}1' "$cert_chain_files > "updated_combined.pem
   #cat $certificate_path
   sed -i.bak -e "/-----END CERTIFICATE-----/r $private_key" -e "/-----END CERTIFICATE-----/d" $certificate_path

   echo "Got new Certificate"
}

推荐答案

我将在此补充我的努力:

Example Script

#!/bin/bash
certfile='chain.txt'
export privfile='private.txt'
export mergefile='output.txt'

touch $mergefile # Create output.txt if it doesn't already exist
true > $mergefile # Truncate output.txt

awk -v occur="1" '
{
    print >> ENVIRON["mergefile"] # Append text to output.txt 
}
/^-----END CERTIFICATE-----$/{
    count++
    
    # If the first occurrence of the match is found, do this
    if(count%occur==0){
        # Read in all lines from private.txt and append to output.txt 
        while((getline line<ENVIRON["privfile"]) > 0) {
           print line >> ENVIRON["mergefile"]
        }
    }
}
' $certfile

cat $mergefile # Output the contents of output.txt to STDOUT

output.txt

-----BEGIN CERTIFICATE-----
... (certificate for your server)...
-----END CERTIFICATE-----
-----BEGIN PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: DES-EDE3-CBC,CFCECC7976725DE5
    <Server Private Key – Passphrase protected>
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
... (the intermediate certificate)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the root certificate for the CA)...
-----END CERTIFICATE----

此脚本使用awk根据正则表达式将chain.txt文件分割成多个单独的部分,并将它们附加到output.txt.当找到第一个匹配项时,它还会将private.txt文件的完整内容附加到output.txt.该脚本不使用临时文件,output.txt文件在开头被截断以清空文件.我使用了一些导出值,以便可以在awk routine 中访问脚本主要部分中定义的文件名.

Linux相关问答推荐

如何创建一堆文件中所有单词的列表?

不带空格的字符串连接形成文件路径

Boost更新失败,现在不确定我有哪个版本

在 Linux 上的 std::threads 中创建子进程

为什么 perf stat 不计算 cycles:u 在 BIOS 中禁用超线程的 Broadwell CPU 上?

命令应在终端关闭后继续运行

Windows WSL 以上 Linux 中的 AF_UNIX 套接字无法绑定到 /mnt 文件:错误 95,不支持操作

sed 命令在 gitlab runner 上无法正确执行

如何用 Sed 替换 Match 后的 2 个连续行

编译过度对齐的动态分配变量时出现 icpc 错误

使用正则表达式时,Shell 'tar: not found in archive' 错误

使用 rc.local 运行脚本:脚本有效,但在启动时无效

无法创建Java虚拟机

如何在非阻塞套接字上处理 OpenSSL SSL_ERROR_WANT_READ / WANT_WRITE

如何在不包括可用空间的情况下创建光盘(sd 卡)的 .IMG 映像?

为什么`du`的输出通常与`du -b`如此不同

将 jiffies 转换为毫秒

ldconfig 错误:使用 Linux 加载程序时不是符号链接

如何拖尾除第一行以外的所有行

Linux 中合理数量的 inotify 监视是多少?