Skip to content

Commit

Permalink
Add password input from environment variable and file (#107)
Browse files Browse the repository at this point in the history
Add password input from environment variable and file
  • Loading branch information
gitseti authored Nov 6, 2019
2 parents e5d1a80 + 79b32c9 commit 0cb56de
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 16 deletions.
12 changes: 8 additions & 4 deletions src/main/java/com/hivemq/cli/commands/AbstractCommonFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import com.hivemq.cli.converters.*;
import com.hivemq.cli.utils.MqttUtils;
import com.hivemq.client.mqtt.MqttClient;
import com.hivemq.client.mqtt.MqttClientSslConfig;
import com.hivemq.client.mqtt.MqttVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.pmw.tinylog.Logger;
Expand All @@ -36,7 +34,6 @@
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public abstract class AbstractCommonFlags extends AbstractConnectRestrictionFlags implements Connect {

Expand All @@ -50,6 +47,14 @@ public abstract class AbstractCommonFlags extends AbstractConnectRestrictionFlag
@Nullable
private ByteBuffer password;

@CommandLine.Option(names = {"-pw:env"}, arity = "0..1", converter = EnvVarToByteBufferConverter.class, fallbackValue = "MQTT_CLI_PW", description = "The password for authentication read in from an environment variable", order = 2)
private void setPasswordFromEnv(final @NotNull ByteBuffer passwordEnvironmentVariable) { password = passwordEnvironmentVariable; }

@CommandLine.Option(names = {"-pw:file"}, converter = FileToByteBufferConverter.class, description = "The password for authentication read in from a file", order = 2)
private void setPasswordFromFile(final @NotNull ByteBuffer passwordFromFile) {
password = passwordFromFile;
}

@CommandLine.Option(names = {"-k", "--keepAlive"}, converter = UnsignedShortConverter.class, description = "A keep alive of the client (in seconds) (default: 60)", order = 2)
private @Nullable Integer keepAlive;

Expand Down Expand Up @@ -83,7 +88,6 @@ public abstract class AbstractCommonFlags extends AbstractConnectRestrictionFlag
@Nullable PrivateKey clientPrivateKey;



public @Nullable MqttClientSslConfig buildSslConfig() {

if (useBuiltSslConfig()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 HiveMQ and the HiveMQ Community
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.hivemq.cli.converters;

import picocli.CommandLine;

import java.nio.ByteBuffer;

public class EnvVarToByteBufferConverter implements CommandLine.ITypeConverter<ByteBuffer> {

static final String ENVIRONMENT_VARIABLE_IS_NULL = "The given environment variable is not defined.";

@Override
public ByteBuffer convert(String value) throws Exception {
final ByteBufferConverter converter = new ByteBufferConverter();
final String envVar = System.getenv(value);

if (envVar == null) {
throw new NullPointerException(ENVIRONMENT_VARIABLE_IS_NULL);
}

return converter.convert(envVar);
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/hivemq/cli/converters/FileConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.hivemq.cli.converters;
/*
* Copyright 2019 HiveMQ and the HiveMQ Community
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import picocli.CommandLine;

import java.io.File;
import java.io.FileNotFoundException;

class FileConverter implements CommandLine.ITypeConverter<File> {

static final String FILE_NOT_FOUND = "The given file was not found.";
static final String NOT_A_FILE = "The given path does not lead to a valid file.";

@Override
public File convert(String value) throws Exception {

final File file = new File(value);

if (!file.exists())
throw new FileNotFoundException(FILE_NOT_FOUND);

if (!file.isFile())
throw new IllegalArgumentException(NOT_A_FILE);


return file;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2019 HiveMQ and the HiveMQ Community
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.hivemq.cli.converters;

import picocli.CommandLine;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.file.Files;

public class FileToByteBufferConverter implements CommandLine.ITypeConverter<ByteBuffer> {
@Override
public ByteBuffer convert(String value) throws Exception {
final FileConverter fileConverter = new FileConverter();

final File file = fileConverter.convert(value);

return ByteBuffer.wrap(Files.readAllBytes(file.toPath()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,13 @@

public class FileToCertificateConverter implements CommandLine.ITypeConverter<X509Certificate> {


static final String FILE_NOT_FOUND = "The given certificate file was not found.";
static final String NO_VALID_FILE_EXTENSION = "The given file does not conform to a valid Certificate File Extension as " + Arrays.toString(CertificateConverterUtils.FILE_EXTENSIONS);
static final String NOT_A_FILE = "The given path is not a valid file.";

@Override
public X509Certificate convert(final @NotNull String s) throws Exception {

final File keyFile = new File(s);

if (!keyFile.exists())
throw new FileNotFoundException(FILE_NOT_FOUND);

if (!keyFile.isFile())
throw new IllegalArgumentException(NOT_A_FILE);
FileConverter fileConverter = new FileConverter();
final File keyFile = fileConverter.convert(s);

final boolean correctExtension = CertificateConverterUtils.endsWithValidExtension(keyFile.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class FileToPrivateKeyConverter implements CommandLine.ITypeConverter<Pri

@Override
public PrivateKey convert(final @NotNull String s) throws Exception {
final File keyFile = new File(s);
final FileConverter fileConverter = new FileConverter();
final File keyFile = fileConverter.convert(s);
return getPrivateKeyFromFile(keyFile);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void convertSuccess() throws Exception {
@Test
void convert_FileNotFound() {
Exception e = assertThrows(FileNotFoundException.class, () -> fileToCertificateConverter.convert("wrongPathXYZ.pem"));
assertEquals(FileToCertificateConverter.FILE_NOT_FOUND, e.getMessage());
assertEquals(FileConverter.FILE_NOT_FOUND, e.getMessage());
}

@Test
Expand Down

0 comments on commit 0cb56de

Please sign in to comment.