# 20190531 maven | password encryption

1. Introduction
2. How to create a master password
3. How to encrypt server passwords
4. How to keep the master password on removable drive
5. Tips

# Introduction

Maven 2.1.0+ now supports server password encryption. The main use case, addressed by this solution is:
• multiple users share the same build machine (server, CI box)
• some users have the privilege to deploy Maven artifacts to repositories, some don’t.
• this applies to any server operations, requiring authorization, not only deployment
• settings.xml is shared between users

The implemented solution adds the following capabilities:

• authorized users have an additional settings-security.xml file in their ${user.home}/.m2 folder • this file either contains encrypted master password, used to encrypt other passwords • or it can contain a relocation - reference to another file, possibly on removable storage • this password is created first via CLI for now • server entries in the settings.xml have passwords and/or keystore passphrases encrypted • for now - this is done via CLI after master password has been created and stored in appropriate location # How to create a master password Use the following command line: Note: Since Maven 3.2.1 the password argument should no longer be used (see Tips below for more information). Maven will prompt for the password. Earlier versions of Maven will not prompt for a password, so it must be typed on the command-line in plaintext. This command will produce an encrypted version of the password, something like Store this password in the ${user.home}/.m2/settings-security.xml; it should look like

When this is done, you can start encrypting existing server passwords.

# How to encrypt server passwords

You will have to use the following command line:

Note:Just like --encrypt-master-password the password argument should no longer be used since Maven 3.2.1 (see Tips below for more information.).

This command will produce an encrypted version of it, something like

Cut-n-paste it into your settings.xml file in the server section. This will look like:

Please note that password can contain any information outside of the curly brackets, so that the following will still work:

Then you can use, say, deploy plugin, to write to this server:

# How to keep the master password on removable drive

Create the master password exactly as described above, and store it on a removable drive, for instance on OSX, my USB drive mounts as /Volumes/mySecureUsb, so I store

in the file /Volumes/mySecureUsb/secure/settings-security.xml

And then I create ${user.home}/.m2/settings-security.xml with the following content: This assures that encryption will only work when the usb drive is mounted by OS. This addresses a use case where only certain people are authorized to deploy and are issued these devices. # Tips ## Escaping curly-brace literals in your password (Since: Maven 2.2.0) At times, you might find that your password (or the encrypted form of it) may actually contain ‘{‘ or ‘}’ as a literal value. If you added such a password as-is to your settings.xml file, you would find that Maven does strange things with it. Specifically, Maven will treat all the characters preceding the ‘{‘ literal, and all the characters after the ‘}’ literal, as comments. Obviously, this is not the behavior you want in such a situation. What you really need is a way of escaping the curly-brace literals in your password. Starting in Maven 2.2.0, you can do just this, with the widely used ‘\‘ escape character. If your password looks like this: Then, the value you would add to your settings.xml would look like this: ## Password Security Editing settings.xml and running the above commands can still leave your password stored locally in plaintext. You may want to check the following locations: • Shell history (e.g. by running history). You may want to clear your history after encrypting the above passwords • Editor caches (e.g. ~/.viminfo) Also note that the encrypted passwords can be decrypted by someone that has the master password and settings security file. Keep this file secure (or stored separately) if you expect the possibility that the settings.xml file may be retrieved. ## Password Escaping on different platforms On some platforms it might be neccessary to quote your password based on the content of your password in particular having special characters like %, !, $ etc. in there. For example on Windows you have to be carefull about things like the following:

The following example will not work on Windows:

whereas the following will work on Windows:

If you are on a linux/unix platform you should use single quotes for the above master password otherwise you will be astonished that the usage of the master-password will not work (caused by the dollar sign and furthermore the exclamation mark).

In Maven before version 3.2.1 you have to give the password on command line as argument which means you might need to escape your password. In addition usually the shell stores the full history of commands you have entered, therefore anyone with access to your computer could restore the password from the shells history.

Starting with Maven 3.2.1 the password is an optional argument which means if you omit the password you will be prompted for it which prevents all the issues mentioned above.

Therefore we strongly recommend to use Maven 3.2.1 and above to prevent problems with escaping special characters and of course security issues related to bash history or environment issues in relationship with the password.

1. 导言（introduction）
2. 如何创建主密码
3. 如何加密服务密码
4. 如何在可移动设备（on removable driver）上保存主密码
5. 小贴士

# 导言（introduction）

• 所有用户共享相同（same）的构建机器（server，CI box）
• 一些用户拥有部署 maven 制品（artifacts）到仓库的特权（privilege）。而一些人不能

• 这适用于（this applies to）任何需要授权的服务器操作，而不仅仅是部署。
• setting.xml 在用户之间分享

• 认证的用户拥有额外的（additional）setting-security.xml 文件在他们的 ${user.home}/.m2 目录 • 这个文件要么包含（either contains）加密的主密码，用于加密其他密码 • 或者它可以包含一个迁移（relocaton）- 引用到另一个文件，也许在可移动存储设备上 • 这个密码先通过（first via） CLI 创建 • setting.xml 中的服务条目加密了密码 或 密钥仓库密码 • 现在，主密码被创建并存储到了合适（appropriate）的位置，这是通过 CLI 完成的。 # 如何创建主密码 使用以下命令 这个命令将会产生一个密码加密之后的版本，比如下面这样： ${user.home}/.m2/settings-security.xml 中存储该密码，像下面这样：

译注：如果没有 settings-security.xml 文件自己创建即可。把内容复制进去。

# 如何在可移动设备上保存主密码

和上文所述（described above）的一样（exactly as）创建主密码，然后存储到可移动设备中，以 OSX 为例，我的 USB 驱动挂载在 /Volumes/mySecureUsb，所以我存储（以下内容）：

/Volumes/mySecureUsb/secure/settings-security.xml