001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.letsencrypt;
029
030import org.opencms.main.CmsLog;
031
032import java.io.FileOutputStream;
033import java.io.IOException;
034import java.net.Socket;
035
036import org.apache.commons.logging.Log;
037
038/**
039 * Updates the certificate configuration for the LetsEncrypt container.<p>
040 */
041public class CmsLetsEncryptUpdater implements I_CmsLetsEncryptUpdater {
042
043    /** The logger instance for this class. */
044    private static final Log LOG = CmsLog.getLog(CmsLetsEncryptUpdater.class);
045
046    /** The LetsEncrypt configuration. */
047    private CmsLetsEncryptConfiguration m_config;
048
049    /**
050     * Creates a new instance.<p>
051     *
052     * @param config the configuration
053     */
054    public CmsLetsEncryptUpdater(CmsLetsEncryptConfiguration config) {
055        m_config = config;
056    }
057
058    /**
059     * @see org.opencms.letsencrypt.I_CmsLetsEncryptUpdater#update(java.lang.String)
060     */
061    public boolean update(String certConfig) {
062
063        if (m_config.isValidAndEnabled()) {
064            LOG.debug("Trying to write certificate configuration: " + certConfig);
065            String certConfigPath = m_config.getCertConfigPath();
066            try (FileOutputStream fos = new FileOutputStream(certConfigPath)) {
067                fos.write(certConfig.getBytes("UTF-8"));
068            } catch (IOException e) {
069                LOG.error("Error writing certificate configuration: " + e.getLocalizedMessage(), e);
070                return false;
071            }
072            String host = m_config.getHost();
073            int port = m_config.getPort();
074            try (Socket socket = new Socket(host, port)) {
075                socket.getOutputStream().write("update".getBytes("UTF-8"));
076            } catch (Exception e) {
077                LOG.error("Couldn't notify LetsEncrypt container: " + e.getLocalizedMessage(), e);
078                return false;
079            }
080            return true;
081        } else {
082            LOG.info("LetsEncrypt configuration is invalid or disabled. ");
083            return false;
084        }
085    }
086
087}