001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.util;
018
019import java.net.URLConnection;
020import java.util.Properties;
021
022import org.apache.logging.log4j.Logger;
023import org.apache.logging.log4j.status.StatusLogger;
024import org.apache.logging.log4j.util.Base64Util;
025import org.apache.logging.log4j.util.LoaderUtil;
026import org.apache.logging.log4j.util.PropertiesUtil;
027
028/**
029 * Provides the Basic Authorization header to a request.
030 */
031public class BasicAuthorizationProvider implements AuthorizationProvider {
032    private static final String[] PREFIXES = {"log4j2.config.", "log4j2.Configuration.", "logging.auth."};
033    private static final String AUTH_USER_NAME = "username";
034    private static final String AUTH_PASSWORD = "password";
035    private static final String AUTH_PASSWORD_DECRYPTOR = "passwordDecryptor";
036    public static final String CONFIG_USER_NAME = "log4j2.configurationUserName";
037    public static final String CONFIG_PASSWORD = "log4j2.configurationPassword";
038    public static final String PASSWORD_DECRYPTOR = "log4j2.passwordDecryptor";
039
040    private static Logger LOGGER = StatusLogger.getLogger();
041
042    private String authString = null;
043
044    public BasicAuthorizationProvider(PropertiesUtil props) {
045        String userName = props.getStringProperty(PREFIXES,AUTH_USER_NAME,
046                () -> props.getStringProperty(CONFIG_USER_NAME));
047        String password = props.getStringProperty(PREFIXES, AUTH_PASSWORD,
048                () -> props.getStringProperty(CONFIG_PASSWORD));
049        String decryptor = props.getStringProperty(PREFIXES, AUTH_PASSWORD_DECRYPTOR,
050                () -> props.getStringProperty(PASSWORD_DECRYPTOR));
051        if (decryptor != null) {
052            try {
053                Object obj = LoaderUtil.newInstanceOf(decryptor);
054                if (obj instanceof PasswordDecryptor) {
055                    password = ((PasswordDecryptor) obj).decryptPassword(password);
056                }
057            } catch (Exception ex) {
058                LOGGER.warn("Unable to decrypt password.", ex);
059            }
060        }
061        if (userName != null && password != null) {
062            authString = "Basic " + Base64Util.encode(userName + ":" + password);
063        }
064    }
065
066    @Override
067    public void addAuthorization(URLConnection urlConnection) {
068        if (authString != null) {
069            urlConnection.setRequestProperty("Authorization", authString);
070        }
071    }
072}