【问题标题】:what's the best way to store username/password for Ant SCP task为 Ant SCP 任务存储用户名/密码的最佳方式是什么
【发布时间】:2020-06-05 02:23:05
【问题描述】:

我需要使用 scp 任务来传输包含许多子文件夹和文件的整个文件夹。目前我使用以下方式:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
        <phase>validate</phase>
        <configuration>
            <tasks>
            <mkdir dir="${project.build.directory}/yy" />                     
            <scp file="user:password@host:/home/xx/yy/*" todir="${project.build.directory}/yy" trust="yes"/>
            </tasks>
        </configuration>
        <goals>
            <goal>run</goal>
        </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.7.1</version>
        </dependency>
    </dependencies>
</plugin>   

如果我硬编码用户名和密码,它也可以工作。但我不想在代码库中公开它。所以我把它存储在 maven-settings.xml 中,如下所示:

pom.xml

<scp file="${scpUserName}:${scpUserPassword}@company.net:/home/xx/yy/*" todir="${project.build.directory}/yy" trust="yes"/>

settings.xml

<profiles>
    <profile>
        <id>unix</id>
        <activation>
            <os>
                <family>unix</family>
            </os>           
        </activation>
        <properties>
            <scpUserName>xxx</scpUserName>
            <scpUserPassword>xxx</scpUserPassword>
        </properties>
    </profile>
</profiles> 

但我仍然需要在 Teamcity 服务器中公开用户名/密码,以便所有代理服务器都可以成功构建整个项目。不知道有没有更好的办法呢?

【问题讨论】:

  • 生成 ssh 密钥,不存储密码。
  • @ElliottFrisch 感谢您的快速回答。但这是否意味着每个构建代理服务器也需要在构建之前安装该密钥?
  • 安装 ssh 密钥需要在 $HOME/.ssh 文件夹中放置两个文件,但是可以。比存储密码要好得多(更安全,更易于管理)。
  • @ElliottFrisch 但是 teamcity 代理服务器在我们公司是不受我们控制的。需要其他方式。谢谢。无论如何。
  • 此传输是 CI 解决方案的工作,而不是 Maven。在您的构建管道中添加一个单独的步骤,让 Teamcity 解决这个问题……还可以使用 TeamCity 的机制来存储/处理凭据。这将使您的 Maven 构建更干净...

标签: java maven ant scp


【解决方案1】:

Maven documentation 解释。首先创建一个主密码

mvn --encrypt-master-password <password>

将结果输入settings-security.xml

<settingsSecurity>
  <master>the result here</master>
</settingsSecurity>

对于个人密码,像这样加密

mvn --encrypt-password <password>

你可以直接在你的 Maven 插件中使用它

<properties>
    <scpUserName>xxx<sscpUserName>
    <scpUserPassword>the result here</scpUserPassword>
</properties>

当您的构建服务器调用 Maven 时,您需要提供主密码。您可以通过使用a password vault 或其他方式存储密码来做到这一点。

【讨论】:

  • 我试了两次。两者都失败了。一个是直接在 pom.xml 中的&lt;scp file="username:{abN4jRWTfosHwLm9ZlsyGzRVQ8aEFcR/pj6bU3fIvus=}@...。第二次在 settings.xml 中设置 {xxx} 并注入回 pom.xml 。再次阅读文档后。我相信这只适用于Maven 2.1.0+ now supports **server password encryption**,不适用于属性。
  • @QingfeiYuan 你能找到解决方案吗? settings-security.xml 最终也会在我的 teamcity 作业中打印加密密码。
猜你喜欢
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 2013-11-24
  • 2017-08-02
  • 1970-01-01
  • 2012-06-23
相关资源
最近更新 更多