/*
 * 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);
    }
}

@@ -1121,96 +1121,8 @@
 der;
-%0A    private final String originalDESCipher;%0A    private final String originalAESCipher;
 %0A%0A  
@@ -1454,687 +1454,175 @@
     
-    try %7B%0A            originalAESCipher = FileUtils.readFileToString(systemEnvironment.getAESCipherFile(), UTF_8);%0A            originalDESCipher = FileUtils.readFileToString(systemEnvironment.getDESCipherFile(), UTF_8);%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected void before() throws Throwable %7B%0A        removeCachedKey();%0A    %7D%0A%0A    @Override%0A    protected void after() %7B%0A        removeCachedKey();%0A        try %7B%0A            setupDESCipherFile(originalDESCipher);%0A            setupAESCipherFile(originalAESCipher);%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D
+%7D%0A%0A    @Override%0A    protected void before() throws Throwable %7B%0A        removeCachedKey();%0A    %7D%0A%0A    @Override%0A    protected void after() %7B%0A        removeCachedKey();
 %0A   

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