001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.scm.provider.git.util; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.regex.Matcher; 024import java.util.regex.Pattern; 025 026import org.apache.commons.lang3.StringUtils; 027import org.apache.maven.scm.providers.gitlib.settings.Settings; 028import org.apache.maven.scm.providers.gitlib.settings.io.xpp3.GitXpp3Reader; 029import org.codehaus.plexus.util.ReaderFactory; 030import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 031 032/** 033 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a> 034 */ 035public class GitUtil { 036 protected static final String GIT_SETTINGS_FILENAME = "git-settings.xml"; 037 038 public static final File DEFAULT_SETTINGS_DIRECTORY = new File(System.getProperty("user.home"), ".scm"); 039 040 /** The password placeholder must contain delimiters. 041 * Otherwise replacing may replace other portions of the URL as well 042 * and in worst case passwords could be guessed. */ 043 public static final String PASSWORD_PLACE_HOLDER_WITH_DELIMITERS = ":********@"; 044 045 private static final Pattern PASSWORD_IN_URL_PATTERN = Pattern.compile("^.*(:[^/].*@).*$"); 046 047 private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY; 048 049 private static Settings settings; 050 051 private GitUtil() { 052 // no op 053 } 054 055 public static Settings getSettings() { 056 if (settings == null) { 057 settings = readSettings(); 058 } 059 return settings; 060 } 061 062 public static Settings readSettings() { 063 File settingsFile = getSettingsFile(); 064 065 if (settingsFile.exists()) { 066 GitXpp3Reader reader = new GitXpp3Reader(); 067 try { 068 return reader.read(ReaderFactory.newXmlReader(settingsFile)); 069 } catch (IOException e) { 070 // Nothing to do 071 } catch (XmlPullParserException e) { 072 String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage(); 073 074 System.err.println(message); 075 } 076 } 077 078 return new Settings(); 079 } 080 081 public static void setSettingsDirectory(File directory) { 082 settingsDirectory = directory; 083 settings = readSettings(); 084 } 085 086 public static File getSettingsFile() { 087 return new File(settingsDirectory, GIT_SETTINGS_FILENAME); 088 } 089 090 /** 091 * Provides an anonymous output to mask password. Considering URL of type : 092 * <<protocol>>://<<user>>:<<password>>@ 093 * <<host_definition>> 094 * 095 * @param urlWithCredentials 096 * @return urlWithCredentials but password masked with stars 097 */ 098 public static String maskPasswordInUrl(String urlWithCredentials) { 099 String output = urlWithCredentials; 100 final Matcher passwordMatcher = PASSWORD_IN_URL_PATTERN.matcher(output); 101 if (passwordMatcher.find()) { 102 // clear password with delimiters 103 final String clearPasswordWithDelimiters = passwordMatcher.group(1); 104 // to be replaced in output by stars with delimiters 105 output = StringUtils.replace(output, clearPasswordWithDelimiters, PASSWORD_PLACE_HOLDER_WITH_DELIMITERS); 106 } 107 return output; 108 } 109}