// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationConstants.java
// public class DataMigrationConstants {
//     public static final String EVENT_PUBLISHER_PATH = "/repository/deployment/server/eventpublishers";
//     public static final String EVENT_RECIEVER_PATH = "/repository/deployment/server/eventreceivers";
//     public static final String CARBON_HOME = "carbon.home";
//     public static final int SUPER_TENANT_ID = -1234;
// }
// 
// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationUtil.java
// public class DataMigrationUtil {
// 
//     private DataMigrationUtil() {
// 
//     }
// 
//     public static boolean isNewlyEncrypted(String encryptedValue) throws DataMigrationException, CryptoException {
//         CryptoUtil cryptoUtil;
//         try {
//             cryptoUtil = CryptoUtil.getDefaultCryptoUtil(CarbonCoreDataHolder.getInstance().
//                             getServerConfigurationService(),
//                     CarbonCoreDataHolder.getInstance().getRegistryService());
//         } catch (Exception e) {
//             throw new DataMigrationException("Error while initializing cryptoUtil", e);
//         }
//         return cryptoUtil.base64DecodeAndIsSelfContainedCipherText(encryptedValue);
//     }
// 
//     public static String reEncryptByNewAlgorithm(String value) throws CryptoException {
//         byte[] decryptedValue = CryptoUtil.getDefaultCryptoUtil().base64DecodeAndDecrypt(
//                 value, "RSA");
//         return CryptoUtil.getDefaultCryptoUtil()
//                 .encryptAndBase64Encode(decryptedValue);
//     }
// }


import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.util.CryptoException;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.migration.exception.DataMigrationException;
import org.wso2.migration.internal.MigrationServiceDataHolder;
import org.wso2.migration.util.DataMigrationConstants;
import org.wso2.migration.util.DataMigrationUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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 org.wso2.migration.service;




/**
 * Migrate user store passwords.
 **/
public class UserStorePasswordMigration extends Migrator {
    private static final Log log = LogFactory.getLog(UserStorePasswordMigration.class);

    @Override
    public void migrate() throws DataMigrationException {
        log.info("Migration starting on Secondary User Stores");
        updateSuperTenantConfigs();
        updateTenantConfigs();
    }

    private void updateTenantConfigs() {
        Tenant[] tenants;
        try {
            tenants = MigrationServiceDataHolder.getRealmService().getTenantManager().getAllTenants();
            for (Tenant tenant : tenants) {
                File[] userstoreConfigs = getUserStoreConfigFiles(tenant.getId());
                for (File file : userstoreConfigs) {
                    if (file.isFile()) {
                        updatePassword(file.getAbsolutePath());
                    }
                }
            }
        } catch (Exception e) {
            log.error("Error while updating secondary user store password for tenant", e);
        }
    }

    private void updateSuperTenantConfigs() {
        try {
// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationConstants.java
// public class DataMigrationConstants {
//     public static final String EVENT_PUBLISHER_PATH = "/repository/deployment/server/eventpublishers";
//     public static final String EVENT_RECIEVER_PATH = "/repository/deployment/server/eventreceivers";
//     public static final String CARBON_HOME = "carbon.home";
//     public static final int SUPER_TENANT_ID = -1234;
// }
// 
// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationUtil.java
// public class DataMigrationUtil {
// 
//     private DataMigrationUtil() {
// 
//     }
// 
//     public static boolean isNewlyEncrypted(String encryptedValue) throws DataMigrationException, CryptoException {
//         CryptoUtil cryptoUtil;
//         try {
//             cryptoUtil = CryptoUtil.getDefaultCryptoUtil(CarbonCoreDataHolder.getInstance().
//                             getServerConfigurationService(),
//                     CarbonCoreDataHolder.getInstance().getRegistryService());
//         } catch (Exception e) {
//             throw new DataMigrationException("Error while initializing cryptoUtil", e);
//         }
//         return cryptoUtil.base64DecodeAndIsSelfContainedCipherText(encryptedValue);
//     }
// 
//     public static String reEncryptByNewAlgorithm(String value) throws CryptoException {
//         byte[] decryptedValue = CryptoUtil.getDefaultCryptoUtil().base64DecodeAndDecrypt(
//                 value, "RSA");
//         return CryptoUtil.getDefaultCryptoUtil()
//                 .encryptAndBase64Encode(decryptedValue);
//     }
// }


// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/service/UserStorePasswordMigration.java
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.util.CryptoException;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.migration.exception.DataMigrationException;
import org.wso2.migration.internal.MigrationServiceDataHolder;
import org.wso2.migration.util.DataMigrationConstants;
import org.wso2.migration.util.DataMigrationUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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 org.wso2.migration.service;




/**
 * Migrate user store passwords.
 **/
public class UserStorePasswordMigration extends Migrator {
    private static final Log log = LogFactory.getLog(UserStorePasswordMigration.class);

    @Override
    public void migrate() throws DataMigrationException {
        log.info("Migration starting on Secondary User Stores");
        updateSuperTenantConfigs();
        updateTenantConfigs();
    }

    private void updateTenantConfigs() {
        Tenant[] tenants;
        try {
            tenants = MigrationServiceDataHolder.getRealmService().getTenantManager().getAllTenants();
            for (Tenant tenant : tenants) {
                File[] userstoreConfigs = getUserStoreConfigFiles(tenant.getId());
                for (File file : userstoreConfigs) {
                    if (file.isFile()) {
                        updatePassword(file.getAbsolutePath());
                    }
                }
            }
        } catch (Exception e) {
            log.error("Error while updating secondary user store password for tenant", e);
        }
    }

    private void updateSuperTenantConfigs() {
        try {
            File[] userstoreConfigs = getUserStoreConfigFiles(DataMigrationConstants.SUPER_TENANT_ID);