001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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
056        m_config = config;
057    }
058
059    /**
060     * @see org.opencms.letsencrypt.I_CmsLetsEncryptUpdater#update(java.lang.String)
061     */
062    public boolean update(String certConfig) {
063
064        if (m_config.isValidAndEnabled()) {
065            LOG.debug("Trying to write certificate configuration: " + certConfig);
066            String certConfigPath = m_config.getCertConfigPath();
067            try (FileOutputStream fos = new FileOutputStream(certConfigPath)) {
068                fos.write(certConfig.getBytes("UTF-8"));
069            } catch (IOException e) {
070                LOG.error("Error writing certificate configuration: " + e.getLocalizedMessage(), e);
071                return false;
072            }
073            String host = m_config.getHost();
074            int port = m_config.getPort();
075            try (Socket socket = new Socket(host, port)) {
076                socket.getOutputStream().write("update".getBytes("UTF-8"));
077            } catch (Exception e) {
078                LOG.error("Couldn't notify LetsEncrypt container: " + e.getLocalizedMessage(), e);
079                return false;
080            }
081            return true;
082        } else {
083            LOG.info("LetsEncrypt configuration is invalid or disabled. ");
084            return false;
085        }
086    }
087
088}