-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move code of CloudSettingAuth to Java
- moved the class's code - moved spec tests to JUnit tests (cherry picked from commit b0920eb)
- Loading branch information
1 parent
44fa849
commit 747c38d
Showing
5 changed files
with
146 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
logstash-core/src/main/java/org/logstash/util/CloudSettingAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 org.logstash.util; | ||
|
||
import co.elastic.logstash.api.Password; | ||
|
||
public class CloudSettingAuth { | ||
|
||
private String original; | ||
private String username; | ||
private Password password; | ||
|
||
public CloudSettingAuth(String value) { | ||
if (value == null) { | ||
return; | ||
} | ||
this.original = value; | ||
final String[] parts = this.original.split(":"); | ||
if (parts.length != 2 || parts[0].isEmpty() || parts[1].isEmpty()) { | ||
throw new IllegalArgumentException("Cloud Auth username and password format should be \"<username>:<password>\"."); | ||
} | ||
|
||
this.username = parts[0]; | ||
this.password = new Password(parts[1]); | ||
} | ||
|
||
public String getOriginal() { | ||
return original; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public Password getPassword() { | ||
return password; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.join(":", username, password.toString()); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
logstash-core/src/test/java/org/logstash/util/CloudSettingAuthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 org.logstash.util; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class CloudSettingAuthTest { | ||
|
||
@Rule | ||
public ExpectedException exceptionRule = ExpectedException.none(); | ||
|
||
@Test | ||
public void testThrowExceptionWhenGivenStringWithoutSeparatorOrPassword() { | ||
exceptionRule.expect(IllegalArgumentException.class); | ||
exceptionRule.expectMessage("Cloud Auth username and password format should be"); | ||
|
||
new CloudSettingAuth("foobarbaz"); | ||
} | ||
|
||
@Test | ||
public void testThrowExceptionWhenGivenStringWithoutPassword() { | ||
exceptionRule.expect(IllegalArgumentException.class); | ||
exceptionRule.expectMessage("Cloud Auth username and password format should be"); | ||
|
||
new CloudSettingAuth("foo:"); | ||
} | ||
|
||
@Test | ||
public void testThrowExceptionWhenGivenStringWithoutUsername() { | ||
exceptionRule.expect(IllegalArgumentException.class); | ||
exceptionRule.expectMessage("Cloud Auth username and password format should be"); | ||
|
||
new CloudSettingAuth(":bar"); | ||
} | ||
|
||
@Test | ||
public void testThrowExceptionWhenGivenStringWhichIsEmpty() { | ||
exceptionRule.expect(IllegalArgumentException.class); | ||
exceptionRule.expectMessage("Cloud Auth username and password format should be"); | ||
|
||
new CloudSettingAuth(""); | ||
} | ||
|
||
@Test | ||
public void testNullInputDoenstThrowAnException() { | ||
new CloudSettingAuth(null); | ||
} | ||
|
||
|
||
@Test | ||
public void testWhenGivenStringWhichIsCloudAuthSetTheString() { | ||
final CloudSettingAuth sut = new CloudSettingAuth("frodo:baggins"); | ||
assertEquals("frodo", sut.getUsername()); | ||
assertEquals("baggins", sut.getPassword().getValue()); | ||
assertEquals("frodo:<password>", sut.toString()); | ||
} | ||
|
||
} |