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 public 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 /** 041 * The password placeholder must contain delimiters. 042 * Otherwise replacing may replace other portions of the URL as well 043 * and in worst case passwords could be guessed. 044 */ 045 public static final String PASSWORD_PLACE_HOLDER_WITH_DELIMITERS = ":********@"; 046 047 private static final Pattern PASSWORD_IN_URL_PATTERN = Pattern.compile("^.*(:[^/].*@).*$"); 048 049 private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY; 050 051 private static Settings settings; 052 053 private GitUtil() { 054 // no op 055 } 056 057 public static Settings getSettings() { 058 if (settings == null) { 059 settings = readSettings(); 060 } 061 return settings; 062 } 063 064 public static Settings readSettings() { 065 File settingsFile = getSettingsFile(); 066 067 if (settingsFile.exists()) { 068 GitXpp3Reader reader = new GitXpp3Reader(); 069 try { 070 return reader.read(ReaderFactory.newXmlReader(settingsFile)); 071 } catch (IOException e) { 072 // Nothing to do 073 } catch (XmlPullParserException e) { 074 String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage(); 075 076 System.err.println(message); 077 } 078 } 079 080 return new Settings(); 081 } 082 083 public static void setSettingsDirectory(File directory) { 084 settingsDirectory = directory; 085 settings = readSettings(); 086 } 087 088 public static File getSettingsFile() { 089 return new File(settingsDirectory, GIT_SETTINGS_FILENAME); 090 } 091 092 /** 093 * Provides an anonymous output to mask password. Considering URL of type : 094 * <<protocol>>://<<user>>:<<password>>@ 095 * <<host_definition>> 096 * 097 * @param urlWithCredentials 098 * @return urlWithCredentials but password masked with stars 099 */ 100 public static String maskPasswordInUrl(String urlWithCredentials) { 101 String output = urlWithCredentials; 102 final Matcher passwordMatcher = PASSWORD_IN_URL_PATTERN.matcher(output); 103 if (passwordMatcher.find()) { 104 // clear password with delimiters 105 final String clearPasswordWithDelimiters = passwordMatcher.group(1); 106 // to be replaced in output by stars with delimiters 107 output = StringUtils.replace(output, clearPasswordWithDelimiters, PASSWORD_PLACE_HOLDER_WITH_DELIMITERS); 108 } 109 return output; 110 } 111}