首页 > 解决方案 > 使用 Scala 访问 SecureString SSM 参数

问题描述

我在 Glue 中使用 Scala 脚本来访问具有依赖库的第三方供应商。你可以在这里看到我正在处理的模板

此解决方案运行良好,但使用以明文形式存储的参数运行。我想将它们移动到 AWS SSM 并将它们存储为 SecureString。为此,我相信该函数必须从 KMS 中提取 CMK,然后提取 SecureString 并使用 CMK 对其进行解密。

我在互联网上四处寻找代码示例,尝试从 Scala 中提取 SSM 参数这样简单的代码示例,但我找不到任何东西。我才刚刚开始使用该语言并且我对它的结构不是很熟悉,是否期望 aws-java 库也可以在 Scala 中用于这些类型的操作?我已经尝试过了,但是在 Glue 中出现编译错误。举个例子

import software.amazon.awscdk.services.ssm.StringParameter;

  
object SfdcExtractData {  
  def main(sysArgs: Array[String]) {  
    print("starting")
    
    String secureStringToken = StringParameter.valueForSecureStringParameter(this, "my-secure-parameter-name", 1);   // must specify version

给出一个编译错误,虽然 aws 胶水不能很好地告诉我问题是什么。

感谢您的时间!如果您有任何代码示例、见解或资源,请告诉我。我的工作是在 Spark 2.4 上运行 Scala 2

标签: javaamazon-web-servicesscalaaws-glueaws-ssm

解决方案


能够使用以下代码片段做到这一点

import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult

    // create a client AWSSimpleSystemsManagementClient object
    val client = new AWSSimpleSystemsManagementClient()

    // Create a GetParameterRequest object, which send the actual request
    val req = new GetParameterRequest()

    // set the name of the parameter in the object. 
    req.setName("test")
    // Only needed if the parameter is a secureString encrypted with the default kms key. If you're using a CMK you need to add the glue user as a key user. To do so, navigate to KMS console --> Customer Managed Keys --> Click on KMS key used for encryption --> Under Key policies --> Key user --> Add ( Add the Glue role )
    req.setWithDecryption(true)


    // call the getParameter() function on the object
    val param = client.getParameter(req)

记得给你的胶水角色 iam 也给 ssm 权限!


推荐阅读