// Path: runAs-common/src/main/java/jetbrains/buildServer/runAs/common/Constants.java
// public class Constants {
//     // Plugin's ids
//     public static final String BUILD_FEATURE_TYPE = "runAs-build-feature";
//     public static final String RUN_AS_TOOL_NAME = "runAs";
// 
//     // Parameter names
//     public static final String USER = "teamcity.runAs.username";
//     public static final String PASSWORD = "secure:teamcity.runAs.password";
//     public static final String CONFIG_PASSWORD = "teamcity.runAs.password";
//     public static final String ADDITIONAL_ARGS = "teamcity.runAs.additionalCommandLine";
//     public static final String RUN_AS_ENABLED = "teamcity.agent.runAs.enabled";
//     public static final String CREDENTIALS_PROFILE_ID = "teamcity.runAs.profileId";
//     public static final String WINDOWS_INTEGRITY_LEVEL = "teamcity.runAs.windowsIntegrityLlevel";
//     public static final String LOGGING_LEVEL = "teamcity.runAs.loggingLevel";
//     public static final String ALLOW_CUSTOM_CREDENTIALS = "teamcity.runAs.allowCustomCredentials";
//     public static final String ALLOW_PROFILE_ID_FROM_SERVER = "teamcity.runAs.allowProfileIdFromServer";
//     public static final String CREDENTIALS_DIRECTORY = "teamcity.runAs.configDir";
//     public static final String RUN_AS_UI_ENABLED = "teamcity.runAs.ui.enabled";
//     public static final String RUN_AS_UI_STEPS_ENABLED = "teamcity.runAs.ui.steps.enabled";
//     public static final String RUN_AS_LOG_ENABLED = "teamcity.runAs.log.enabled";
//     public static final String RUN_AS_ACL = "teamcity.runAs.acl";
//     public static final String RUN_AS_ACL_DEFAULTS_ENABLED = "teamcity.runAs.acl.defaults.enabled";
// }


import java.io.File;
import java.util.*;
import jetbrains.buildServer.agent.BuildAgentSystemInfo;
import jetbrains.buildServer.dotNet.buildRunner.agent.*;
import jetbrains.buildServer.runAs.common.Constants;
import org.jetbrains.annotations.NotNull;
    final File commandFile = myFileService.getTempFileName(myCommandFileExtension);
    resources.add(new CommandLineFile(myBeforeBuildPublisher, commandFile.getAbsoluteFile(), myRunAsCmdGenerator.create(params)));
    final ArrayList<AccessControlEntry> acl = new ArrayList<AccessControlEntry>();
    for (AccessControlEntry ace: myAccessControlListProvider.getAcl(userCredentials)) {
      acl.add(ace);
    }

    acl.add(new AccessControlEntry(commandFile, AccessControlAccount.forUser(userCredentials.getUser()), EnumSet.of(AccessPermissions.GrantExecute), AccessControlScope.Step));

    final File runAsToolPath = getTool();
    final AccessControlEntry runAsToolAce = new AccessControlEntry(runAsToolPath, AccessControlAccount.forUser(userCredentials.getUser()), EnumSet.of(AccessPermissions.GrantExecute), AccessControlScope.Build);
    acl.add(runAsToolAce);

    myAccessControlResource.setAcl(new AccessControlList(acl));
    resources.add(myAccessControlResource);

    final CommandLineSetup runAsCommandLineSetup = new CommandLineSetup(
      runAsToolPath.getAbsolutePath(),
      Arrays.asList(
        new CommandLineArgument(settingsFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER),
        new CommandLineArgument(commandFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER),
        new CommandLineArgument(myBuildAgentSystemInfo.bitness().toString(), CommandLineArgument.Type.PARAMETER),
        new CommandLineArgument(myArgumentConverter.convert(userCredentials.getPassword()), CommandLineArgument.Type.PARAMETER)),
      resources);

    myRunAsLogger.LogRunAs(userCredentials, commandLineSetup, runAsCommandLineSetup);
    return Collections.singleton(runAsCommandLineSetup);
  }

  private File getTool() {
// Path: runAs-common/src/main/java/jetbrains/buildServer/runAs/common/Constants.java
// public class Constants {
//     // Plugin's ids
//     public static final String BUILD_FEATURE_TYPE = "runAs-build-feature";
//     public static final String RUN_AS_TOOL_NAME = "runAs";
// 
//     // Parameter names
//     public static final String USER = "teamcity.runAs.username";
//     public static final String PASSWORD = "secure:teamcity.runAs.password";
//     public static final String CONFIG_PASSWORD = "teamcity.runAs.password";
//     public static final String ADDITIONAL_ARGS = "teamcity.runAs.additionalCommandLine";
//     public static final String RUN_AS_ENABLED = "teamcity.agent.runAs.enabled";
//     public static final String CREDENTIALS_PROFILE_ID = "teamcity.runAs.profileId";
//     public static final String WINDOWS_INTEGRITY_LEVEL = "teamcity.runAs.windowsIntegrityLlevel";
//     public static final String LOGGING_LEVEL = "teamcity.runAs.loggingLevel";
//     public static final String ALLOW_CUSTOM_CREDENTIALS = "teamcity.runAs.allowCustomCredentials";
//     public static final String ALLOW_PROFILE_ID_FROM_SERVER = "teamcity.runAs.allowProfileIdFromServer";
//     public static final String CREDENTIALS_DIRECTORY = "teamcity.runAs.configDir";
//     public static final String RUN_AS_UI_ENABLED = "teamcity.runAs.ui.enabled";
//     public static final String RUN_AS_UI_STEPS_ENABLED = "teamcity.runAs.ui.steps.enabled";
//     public static final String RUN_AS_LOG_ENABLED = "teamcity.runAs.log.enabled";
//     public static final String RUN_AS_ACL = "teamcity.runAs.acl";
//     public static final String RUN_AS_ACL_DEFAULTS_ENABLED = "teamcity.runAs.acl.defaults.enabled";
// }


// Path: runAs-agent/src/main/java/jetbrains/buildServer/runAs/agent/RunAsPlatformSpecificSetupBuilder.java
import java.io.File;
import java.util.*;
import jetbrains.buildServer.agent.BuildAgentSystemInfo;
import jetbrains.buildServer.dotNet.buildRunner.agent.*;
import jetbrains.buildServer.runAs.common.Constants;
import org.jetbrains.annotations.NotNull;

    final File commandFile = myFileService.getTempFileName(myCommandFileExtension);
    resources.add(new CommandLineFile(myBeforeBuildPublisher, commandFile.getAbsoluteFile(), myRunAsCmdGenerator.create(params)));
    final ArrayList<AccessControlEntry> acl = new ArrayList<AccessControlEntry>();
    for (AccessControlEntry ace: myAccessControlListProvider.getAcl(userCredentials)) {
      acl.add(ace);
    }

    acl.add(new AccessControlEntry(commandFile, AccessControlAccount.forUser(userCredentials.getUser()), EnumSet.of(AccessPermissions.GrantExecute), AccessControlScope.Step));

    final File runAsToolPath = getTool();
    final AccessControlEntry runAsToolAce = new AccessControlEntry(runAsToolPath, AccessControlAccount.forUser(userCredentials.getUser()), EnumSet.of(AccessPermissions.GrantExecute), AccessControlScope.Build);
    acl.add(runAsToolAce);

    myAccessControlResource.setAcl(new AccessControlList(acl));
    resources.add(myAccessControlResource);

    final CommandLineSetup runAsCommandLineSetup = new CommandLineSetup(
      runAsToolPath.getAbsolutePath(),
      Arrays.asList(
        new CommandLineArgument(settingsFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER),
        new CommandLineArgument(commandFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER),
        new CommandLineArgument(myBuildAgentSystemInfo.bitness().toString(), CommandLineArgument.Type.PARAMETER),
        new CommandLineArgument(myArgumentConverter.convert(userCredentials.getPassword()), CommandLineArgument.Type.PARAMETER)),
      resources);

    myRunAsLogger.LogRunAs(userCredentials, commandLineSetup, runAsCommandLineSetup);
    return Collections.singleton(runAsCommandLineSetup);
  }

  private File getTool() {
    final File path = new File(myRunnerParametersService.getToolPath(Constants.RUN_AS_TOOL_NAME), TOOL_FILE_NAME + myCommandFileExtension);