有关在启用 TLS 的 Elasticache memcached 集群中设置 Node js 客户端的问题

问题描述 投票:0回答:2

我正在将负载生成器设置到elasticache memcached集群中https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/WhatIs.html以测试分布式sts假定角色的退化情况缓存。

我的负载生成器的工作方式是这样的

  1. 我在我的 Elasticache 集群所在的同一 vpc 中的 ec2 实例中承担一次 AWS STS 角色。
  2. 我创建一个键值对,其中我的键是 uuid,我的值是序列化的凭据。
  3. 然后生成 n k,v 对,其中 n 是我可以作为命令行参数提供的配置。
  4. 我将每个 k,v 对放入一个 memcache 集群中。

我使用 memjs https://www.npmjs.com/package/memjs 将信用信息放入其中。

到目前为止一切顺利,但我很好奇如何让它与 https://aws.amazon.com/about-aws/whats-new/2022/05/amazon-elasticache-memcached-supports-encryption 一起使用-数据传输/

看起来我不需要自定义证书链:https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/in-transit-encryption-mc.html并且我得到了基本连接去工作。

但是我不知道如何在 Node js 中执行此操作。有人可以帮我解决这个问题吗?

我的负载生成器的完整代码是

// Script to fill up an elasticache cluster with credentials

const commandLineUsage = require('command-line-usage')
const commandLineArgs = require('command-line-args')
const optionDefinitions = [
    { name: 'clusterEndpoint', type: String, description: "Elasticache (Memcached) Cluster to set credentials into. In host:port format" },
    { name: 'clusterRegion', type: String, description: "Region where cluster lives" },
    { name: 'roleArnToAssume', type: String, description: "STS Role ARN to assume. Assumed role creds will be uploaded to Elastiache" },
    { name: 'cacheTtlInSeconds', type: Number, description: "Time to live in STS Cache" },
    { name: 'numCredsToUpload', type: Number, description: "Number of times to assume role and upload credentials" },
]

const options = commandLineArgs(optionDefinitions)
console.log("Seen args")
console.log(options)

if (options.clusterEndpoint === undefined ||
    options.roleArnToAssume === undefined ||
    options.cacheTtlInSeconds === undefined ||
    options.numCredsToUpload === undefined ||
    options.clusterRegion === undefined) {
    console.log("Invalid parameters. Please provide all options below")
    const usage = commandLineUsage([
        {
            header: 'Options',
            optionList: optionDefinitions
        }
    ])
    console.log(usage)
} else {
    const clusterEndpoint = options.clusterEndpoint
    const roleArnToAssume = options.roleArnToAssume
    const cacheTtlInSeconds = options.cacheTtlInSeconds
    const numCredsToUpload = options.numCredsToUpload
    const clusterRegion = options.clusterRegion

    // Setup memcached client
    const memjs = require('memjs')
    const memcachedClient = memjs.Client.create(clusterEndpoint)

    // Setup STS client
    const sts = require('@aws-sdk/client-sts')
    const stsClient = new sts.STSClient({ region: clusterRegion })

    const serialize = require('serialize-javascript')
    const { v4: uuidv4 } = require('uuid')
    const stsAssumeRoleCommand = new sts.AssumeRoleCommand({
        RoleArn: roleArnToAssume,
        RoleSessionName: "SessionName"
    })
    const stsAssumedRole = stsClient.send(stsAssumeRoleCommand)

    for (var i = 0; i < numCredsToUpload; i++) {
        const key = uuidv4()
        stsAssumedRole.then(
            (data) => {
                const dataToPutIn = serialize(data.Credentials)
                return memcachedClient.set(key, dataToPutIn, { expires:cacheTtlInSeconds }, (err, _) => {
                    if (err !== null) {
                        console.log("Caught error while setting creds " + err.toString())
                        throw err
                    }
                    console.log("Successfully put memcached key : " + key + " and value " + dataToPutIn)
                })
            }).catch((error) => {
                throw error
            }).finally(() => {
                // finally.
            }
        )
    }
}

我的依赖关闭是

{
  "dependencies": {
    "@aws-sdk/client-sts": "^3.245.0",
    "command-line-args": "^5.2.1",
    "command-line-usage": "^6.1.3",
    "memcached": "^2.2.2",
    "memjs": "^1.3.0",
    "serialize-javascript": "^6.0.1",
    "uuid": "^8.3.2"
  }
}

我一半期待 memjs 客户端 init 中的一些属性,但我似乎找不到它。是否有替代方法/机制可以侦听此流量然后对其进行加密(如果说这个或任何其他库不支持 tls)。

node.js memcached amazon-elasticache
2个回答
2
投票

根据对现有 OSS 客户端(memjs、node-memcached、memcached、mc、nodejs-memcache)的审查,目前似乎没有任何支持 TLS 的 NodeJS Memcached 客户端。

您可以使用nodeJS“tls”模块通过TLS连接到Memcached服务器,但您需要手动创建和解析Memcached API请求/响应。 这是一个 NodeJS TLS 客户端的示例。

如果 Java 或 PHP 适用,您可以使用带有 TLS 的 ElastiCache Memcached 客户端。

下载 ElastiCache Memcached 集群客户端:

  1. 登录 Amazon Web Services 管理控制台并通过以下网址打开 ElastiCache 控制台 https://console.amazonaws.cn/elasticache/

  2. 从 ElastiCache 控制台中,选择 ElastiCache 集群客户端。

  3. 从下载 ElastiCache Memcached 集群客户端列表中,选择与您的首选语言、版本和 AMI 架构相匹配的 ElastiCache 集群客户端,然后选择下载按钮。

更多相关链接:


0
投票

我们最近为 electrode-io memcache 客户端 提供了 TLS 支持。除了 NodeJS 之外,它还支持 Typescript。也可以在 NPM 上找到。

如果memcached服务器配置了TLS,您可以通过指定

tls
ConnectionOptions让客户端连接到它。对于生产环境,服务器应使用由受信任的公共 CA 签名的 TLS 证书。在 在这种情况下,您可以简单地执行以下操作来创建客户端:

import MemcacheClient from "memcache-client";
const client = new MemcacheClient({server: "{server_hostname}:11211", tls: {}});
client.set("key", "value");

如果服务器需要客户端证书认证,可以执行以下操作:

import MemcacheClient from "memcache-client";
import Fs from "fs";
const client = new MemcacheClient({server: "{server_hostname}:11211", tls: {
  key: Fs.readFileSync("client-key.pem"),
  cert: Fs.readFileSync("client-cert.pem"),
}});
client.set("key", "value");

如果您使用自签名证书运行服务器(即用于本地开发),您可以创建客户端 通过指定 CA 证书并禁用主机名验证,如下所示:

import MemcacheClient from "memcache-client";
import Fs from "fs";
const client = new MemcacheClient({server: "localhost:11211", tls: {
  ca: Fs.readFileSync("ca-cert.pem"),
  checkServerIdentity: () => {return undefined;}
}});
client.set("key", "value");

此外,请注意,memjs 客户端仅支持二进制协议,该协议自 memcached 1.6 起已被弃用。您可能需要考虑使用支持 ASCII 协议的 memcached 客户端。

Pymemcache 客户端也是一个不错的选择。它有 TLS 支持

© www.soinside.com 2019 - 2024. All rights reserved.