This is an automated email from the ASF dual-hosted git repository.
aonishuk pushed a commit to branch branch-2.7
in repository https://gitbox.apache.org/repos/asf/ambari.git
commit 9a1c24863dee62d769c0e3d80e8daa25be0fc17c
Author: Andrew Onishuk
AuthorDate: Thu Nov 28 13:03:54 2019 +0200
AMBARI-25433. Ambari should add login and password to urls populated from
VDF (aonishuk)
---
.../VersionDefinitionResourceProvider.java | 14 +++-
.../ambari/server/state/stack/RepositoryXml.java | 25 --
2 files changed, 36 insertions(+), 3 deletions(-)
diff --git
a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProvider.java
index 9882147..97cf917 100644
---
a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProvider.java
+++
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProvider.java
@@ -19,7 +19,9 @@ package org.apache.ambari.server.controller.internal;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -29,6 +31,8 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.ambari.annotations.Experimental;
import org.apache.ambari.annotations.ExperimentalFeature;
@@ -611,7 +615,15 @@ public class VersionDefinitionResourceProvider extends
AbstractAuthorizedResourc
entity.setStack(stackEntity);
-List repos = holder.xml.repositoryInfo.getRepositories();
+String credentials;
+try {
+ URL url = new URL(holder.url);
+ credentials = url.getUserInfo();
+} catch (MalformedURLException e) {
+ throw new AmbariException(String.format("Could not parse url %s",
holder.url), e);
+}
+
+List repos =
holder.xml.repositoryInfo.getRepositories(credentials);
// Add service repositories (these are not contained by the VDF but are
there in the stack model)
ListMultimap stackReposByOs =
diff --git
a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/RepositoryXml.java
b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/RepositoryXml.java
index ccb25e8..c872deb 100644
---
a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/RepositoryXml.java
+++
b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/RepositoryXml.java
@@ -22,7 +22,8 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
-
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@@ -31,6 +32,7 @@ import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
+import com.google.common.base.Strings;
import org.apache.ambari.server.stack.Validable;
import org.apache.ambari.server.state.RepositoryInfo;
@@ -40,6 +42,7 @@ import org.apache.ambari.server.state.RepositoryInfo;
@XmlRootElement(name="reposinfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class RepositoryXml implements Validable{
+ private static final Pattern HTTP_URL_PROTOCOL_PATTERN =
Pattern.compile("((http(s)*:\\/\\/))");
@XmlElement(name="latest")
private String latestUri;
@@ -219,6 +222,16 @@ public class RepositoryXml implements Validable{
* @return the list of repositories consumable by the web service.
*/
public List getRepositories() {
+return getRepositories(null);
+ }
+
+ /**
+ * @param credentials string with column separated username and password to
be inserted in basurl.
+ *If set to null baseurl is not changed.
+ *
+ * @return the list of repositories consumable by the web service.
+ */
+ public List getRepositories(String credentials) {
List repos = new ArrayList<>();
for (RepositoryXml.Os o : getOses()) {
@@ -227,7 +240,15 @@ public class RepositoryXml implements Validable{
for (RepositoryXml.Repo r : o.getRepos()) {
RepositoryInfo ri = new RepositoryInfo();
- ri.setBaseUrl(r.getBaseUrl());
+ String baseUrl = r.getBaseUrl();
+
+ // add credentials from VDF url to baseurl.
+ if (!Strings.isNullOrEmpty(credentials)) {
+Matcher matcher = HTTP_URL_PROTOCOL_PATTERN.matcher(baseUrl);
+baseUrl = matcher.replaceAll("$1" + credentials + "@");
+ }
+
+ ri.setBaseUrl(baseUrl);