我正在用 Python 生成 puppet hieradata yml 文件。
我想用 Python 实现以下 Ruby 代码...
public_key_pem = File.read(puppet_key_file)
@public_key_x509 = OpenSSL::X509::Certificate.new( public_key_pem )
@cipher = OpenSSL::Cipher::AES.new(256, :CBC)
ciphertext = OpenSSL::PKCS7::encrypt([@public_key_x509],
plaintext,
@cipher,
OpenSSL::PKCS7::BINARY
).to_der
"ENC[PKCS7,#{Base64.encode64(ciphertext).gsub("\n", '')}]"
Run Code Online (Sandbox Code Playgroud)
在另一个模块中,我正在使用 PyOpenssl 处理证书签名和密钥生成,但我注意到加密方法没有实现......
M2Crypto 的潜在解决方案...
from M2Crypto import SMIME, X509, BIO
def encrypt(self, plaintext):
"""
Encrypt a string using the previously generated public key AES-256-CBC, SMIME PKCS7 envelop
:param plaintext: The text to encrypt
:type plaintext: str
:returns: The encrypted text
:rtype: str
"""
buf = BIO.MemoryBuffer(plaintext) …Run Code Online (Sandbox Code Playgroud) 我想问一下我们应该如何处理 Golang 中上下文传播的问题。
我的应用程序是一个 HTTP JSON API 服务器。
我会将上下文用作信息数据的容器(例如,请求 ID,我从请求或流程中解压出来的一些东西)。
最愚蠢的优势之一是车辆数据和标签可用于统计和记录。例如,能够在每个日志行添加我拥有的所有包中的事务 ID
我面临的问题如下:
func handleActivityY(w http.ResponseWriter, r *http.Request) {
info, err := decodeRequest(r)
...
stepOne, err := stepOne(r.Context(), info)
...
stepTwo, err := stepTwo(r.Context(), stepOne)
...
}
Run Code Online (Sandbox Code Playgroud)
这种设计的问题在于上下文是一个不可变的实体(每次我们添加内容或设置新的超时时,我们都有一个新的上下文)。
除非在每次函数调用时返回上下文(连同返回值,如果有的话和错误),否则不能传播上下文。
使这项工作的唯一方法是:
func handleActivityY(w http.ResponseWriter, r *http.Request) {
ctx, info, err := decodeRequest(r)
...
ctx, stepOne, err := stepOne(ctx, info)
...
ctx, stepTwo, err := stepTwo(ctx, stepOne)
...
}
Run Code Online (Sandbox Code Playgroud)
我已经使用参数污染了包中的几乎所有函数context.Context。除了其他参数外还返回它在我看来有点矫枉过正。
真的没有其他更优雅的方法吗?
我目前正在使用 gin 框架,它有自己的上下文并且是可变的。我不想为此添加对 Gin 的依赖。