1

我有一个用 Groovy DSL 编写的 Ratpack 应用程序。(嵌入在 Java 中,所以不是脚本。)

我想从命令行选项中提供的配置文件中加载服务器的 SSL 证书。(证书将直接嵌入到配置中,或者可能在配置中某处引用的 PEM 文件中。)

例如:

java -jar httpd.jar /etc/app/sslConfig.yml

sslConfig.yml:

---
ssl:
    privateKey: file:///etc/app/privateKey.pem
    certChain: file:///etc/app/certChain.pem

我似乎有一个先有鸡还是先有蛋的问题serverConfigSslContext使用serverConfig. 在我要加载 SslContext 时未创建服务器配置。

为了说明,我的 DSL 定义是这样的:

   // SSL Config POJO definition
   class SslConfig {
       String privateKey
       String certChain
       SslContext build() { /* ... */ }
   }

   // ... other declarations here...

   Path configPath = Paths.get(args[1]) // get this path from the CLI options

   ratpack {
        serverConfig {
            yaml "/defaultConfig.yaml" // Defaults defined in this resource
            yaml configPath // The user-supplied config file

            env()
            sysProps('genset-server')

            require("/ssl", SslConfig) // Map the config to a POJO

            ssl sslConfig // HOW DO I GET AN INSTANCE OF that SslConfig POJO HERE?
            baseDir BaseDir.find()
        }

        handlers {
            get { // ... 
            }
        }
   }

可能有一个解决方案(在稍后的块中加载 SSL 上下文?)

或者可能只是一个更好的方式来处理整个事情..?

4

2 回答 2

3

您可以创建一个单独ConfigDataBuilder的来加载配置对象以反序列化您的 ssl 配置。

或者,您可以直接绑定到server.ssl. 所有ServerConfig属性都绑定到server配置中的空间。

于 2018-03-23T00:54:25.870 回答
0

我目前使用的解决方案是这个,添加了一个#builder方法,SslConfig该方法返回一个SslContextBuilder使用其他字段定义的方法。

ratpack {
    serverConfig {
       // Defaults defined in this resource
       yaml RatpackEntryPoint.getResource("/defaultConfig.yaml") 

       // Optionally load the config path passed via the configFile parameter (if not null)
        switch (configPath) {
            case ~/.*[.]ya?ml/: yaml configPath; break
            case ~/.*[.]json/: json configPath; break
            case ~/.*[.]properties/: props configPath; break
        }

        env()
        sysProps('genset-server')

        require("/ssl", SslConfig) // Map the config to a POJO

        baseDir BaseDir.find()

        // This is the important change.
        // It apparently needs to come last, because it prevents
        // later config directives working without errors
        ssl build().getAsConfigObject('/ssl',SslConfig).object.builder().build()
    }

    handlers {
        get { // ... 
        }
    }

}

本质上,这执行了一个额外的构建,ServerConfig以便重新定义第二个构建的输入,但它可以工作。

于 2018-03-29T09:25:45.043 回答