/*
 * Copyright 2018 ThoughtWorks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thoughtworks.go.security;

import com.thoughtworks.go.util.ReflectionUtil;
import com.thoughtworks.go.util.SystemEnvironment;
import org.apache.commons.io.FileUtils;
import org.junit.rules.ExternalResource;

import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;

public class ResetCipher extends ExternalResource {
    private final SystemEnvironment systemEnvironment;
    private final AESCipherProvider aesCipherProvider;
    private final DESCipherProvider desCipherProvider;
    private final String originalDESCipher;
    private final String originalAESCipher;

    public ResetCipher() {
        this(new SystemEnvironment());
    }

    public ResetCipher(SystemEnvironment systemEnvironment) {
        this.systemEnvironment = systemEnvironment;
        aesCipherProvider = new AESCipherProvider(systemEnvironment);
        desCipherProvider = new DESCipherProvider(systemEnvironment);
        try {
            originalAESCipher = FileUtils.readFileToString(systemEnvironment.getAESCipherFile(), UTF_8);
            originalDESCipher = FileUtils.readFileToString(systemEnvironment.getDESCipherFile(), UTF_8);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void before() throws Throwable {
        removeCachedKey();
    }

    @Override
    protected void after() {
        removeCachedKey();
        try {
            setupDESCipherFile(originalDESCipher);
            setupAESCipherFile(originalAESCipher);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void removeCachedKey() {
        aesCipherProvider.removeCachedKey();
        desCipherProvider.removeCachedKey();
    }

    public void setupAESCipherFile() throws IOException {
        setupAESCipherFile("fdf500c4ec6e51172477145db6be63c5");
    }

    public void setupAESCipherFile(String cipher) throws IOException {
        ReflectionUtil.setField(aesCipherProvider, "cachedKey", null);
        FileUtils.writeStringToFile(systemEnvironment.getAESCipherFile(), cipher, UTF_8);
    }

    public void setupDESCipherFile() throws IOException {
        setupDESCipherFile("269298bc31c44620");
    }

    public void setupDESCipherFile(String cipher) throws IOException {
        ReflectionUtil.setField(desCipherProvider, "cachedKey", null);
        FileUtils.writeStringToFile(systemEnvironment.getDESCipherFile(), cipher, UTF_8);
    }
}

/*
 * Copyright 2018 ThoughtWorks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thoughtworks.go.security;

import com.thoughtworks.go.util.ReflectionUtil;
import com.thoughtworks.go.util.SystemEnvironment;
import org.apache.commons.io.FileUtils;
import org.junit.rules.ExternalResource;

import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;

public class ResetCipher extends ExternalResource {
    private final SystemEnvironment systemEnvironment;
    private final AESCipherProvider aesCipherProvider;
    private final DESCipherProvider desCipherProvider;

    public ResetCipher() {
        this(new SystemEnvironment());
    }

    public ResetCipher(SystemEnvironment systemEnvironment) {
        this.systemEnvironment = systemEnvironment;
        aesCipherProvider = new AESCipherProvider(systemEnvironment);
        desCipherProvider = new DESCipherProvider(systemEnvironment);
    }

    @Override
    protected void before() throws Throwable {
        removeCachedKey();
    }

    @Override
    protected void after() {
        removeCachedKey();
    }

    private void removeCachedKey() {
        aesCipherProvider.removeCachedKey();
        desCipherProvider.removeCachedKey();
    }

    public void setupAESCipherFile() throws IOException {
        setupAESCipherFile("fdf500c4ec6e51172477145db6be63c5");
    }

    public void setupAESCipherFile(String cipher) throws IOException {
        ReflectionUtil.setField(aesCipherProvider, "cachedKey", null);
        FileUtils.writeStringToFile(systemEnvironment.getAESCipherFile(), cipher, UTF_8);
    }

    public void setupDESCipherFile() throws IOException {
        setupDESCipherFile("269298bc31c44620");
    }

    public void setupDESCipherFile(String cipher) throws IOException {
        ReflectionUtil.setField(desCipherProvider, "cachedKey", null);
        FileUtils.writeStringToFile(systemEnvironment.getDESCipherFile(), cipher, UTF_8);
    }
}

Remove code to reset original ciphers. We shouldn't need that since the ciphers aren't committed in the repo.