public static void Delete(ICollection <AutomationAsset> assetsToDelete, String localWorkspacePath, AutomationManagementClient automationApi, string resourceGroupName, string automationAccountName, bool deleteLocally, bool deleteFromCloud, String encryptionCertThumbprint, ICollection <ConnectionType> connectionTypes)
        {
            if (deleteLocally)
            {
                LocalAssetsStore.Delete(localWorkspacePath, assetsToDelete, encryptionCertThumbprint, connectionTypes);
            }

            if (deleteFromCloud)
            {
                foreach (var assetToDelete in assetsToDelete)
                {
                    if (assetToDelete.LastModifiedCloud == null)
                    {
                        // asset is local only, no need to delete it from cloud
                        continue;
                    }
                    if (assetToDelete is AutomationVariable)
                    {
                        automationApi.Variables.Delete(resourceGroupName, automationAccountName, assetToDelete.Name);
                    }
                    else if (assetToDelete is AutomationCredential)
                    {
                        automationApi.PsCredentials.Delete(resourceGroupName, automationAccountName, assetToDelete.Name);
                    }
                    else if (assetToDelete is AutomationCertificate)
                    {
                        automationApi.Certificates.Delete(resourceGroupName, automationAccountName, assetToDelete.Name);
                    }
                }
            }
        }
        // updates the local assets, either removing (if replace = false) or adding/replacing (if replace = true) the specified assets
        private static void Set(String workspacePath, ICollection <AutomationAsset> assetsToAffect, bool replace, String encryptionCertThumbprint)
        {
            LocalAssets localAssets = LocalAssetsStore.Get(workspacePath, encryptionCertThumbprint);

            foreach (var assetToAffect in assetsToAffect)
            {
                AssetJson assetToDelete = LocalAssetsStore.FindMatchingLocalAsset(localAssets, assetToAffect);

                if (assetToAffect is AutomationVariable)
                {
                    var variableToAffect = (AutomationVariable)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var variableToDelete = (VariableJson)assetToDelete;

                        // Encrypted variable assets returned from the cloud have their values removed,
                        // so keep the old local asset value instead of overwriting the local asset value with null
                        if (variableToAffect.Encrypted && variableToAffect.getValue() == null)
                        {
                            variableToAffect.setValue(variableToDelete.Value);
                        }

                        localAssets.Variables.Remove(variableToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Variables.Add(new VariableJson(variableToAffect));
                    }
                }

                else if (assetToAffect is AutomationCredential)
                {
                    var credToAffect = (AutomationCredential)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var credToDelete = (CredentialJson)assetToDelete;

                        // PSCredential assets returned from the cloud have their passwords removed,
                        // so keep the old local asset password instead of overwriting the local asset password with null
                        if (credToAffect.getPassword() == null)
                        {
                            credToAffect.setPassword(credToDelete.Password);
                        }

                        localAssets.PSCredentials.Remove(credToDelete);
                    }

                    if (replace)
                    {
                        localAssets.PSCredentials.Add(new CredentialJson(credToAffect));
                    }
                }

                else if (assetToAffect is AutomationConnection)
                {
                    // TODO: support carry over of null fields from previous local asset

                    if (assetToDelete != null)
                    {
                        localAssets.Connections.Remove((ConnectionJson)assetToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Connections.Add(new ConnectionJson((AutomationConnection)assetToAffect));
                    }
                }
            }

            DirectoryInfo dir = Directory.CreateDirectory(workspacePath);

            UnsecureLocalAssetsContainerJson.Set(workspacePath, localAssets);
            SecureLocalAssetsContainerJson.Set(workspacePath, localAssets, encryptionCertThumbprint);
        }
 public static void Delete(String workspacePath, ICollection <AutomationAsset> assetsToDelete, String encryptionCertThumbprint)
 {
     LocalAssetsStore.Set(workspacePath, assetsToDelete, false, encryptionCertThumbprint);
 }
 public static void Add(String workspacePath, ICollection <AutomationAsset> newAssets, String encryptionCertThumbprint)
 {
     LocalAssetsStore.Set(workspacePath, newAssets, true, encryptionCertThumbprint);
 }
        // updates the local assets, either removing (if replace = false) or adding/replacing (if replace = true) the specified assets
        private static void Set(String workspacePath, ICollection <AutomationAsset> assetsToAffect, bool replace, String encryptionCertThumbprint, ICollection <ConnectionType> connectionTypes)
        {
            LocalAssets localAssets = LocalAssetsStore.Get(workspacePath, encryptionCertThumbprint, connectionTypes);

            foreach (var assetToAffect in assetsToAffect)
            {
                AssetJson assetToDelete = LocalAssetsStore.FindMatchingLocalAsset(localAssets, assetToAffect);

                if (assetToAffect is AutomationVariable)
                {
                    var variableToAffect = (AutomationVariable)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var variableToDelete = (VariableJson)assetToDelete;

                        // Encrypted variable assets returned from the cloud have their values removed,
                        // so keep the old local asset value instead of overwriting the local asset value with null
                        if (variableToAffect.Encrypted && variableToAffect.getValue() == null)
                        {
                            variableToAffect.setValue(variableToDelete.Value);
                        }

                        localAssets.Variables.Remove(variableToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Variables.Add(new VariableJson(variableToAffect));
                    }
                }

                else if (assetToAffect is AutomationCredential)
                {
                    var credToAffect = (AutomationCredential)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var credToDelete = (CredentialJson)assetToDelete;

                        // PSCredential assets returned from the cloud have their passwords removed,
                        // so keep the old local asset password instead of overwriting the local asset password with null
                        if (credToAffect.getPassword() == null)
                        {
                            credToAffect.setPassword(credToDelete.Password);
                        }

                        localAssets.PSCredentials.Remove(credToDelete);
                    }

                    if (replace)
                    {
                        localAssets.PSCredentials.Add(new CredentialJson(credToAffect));
                    }
                }

                else if (assetToAffect is AutomationConnection)
                {
                    var connectionToAffect = (AutomationConnection)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var connectionToDelete = (ConnectionJson)assetToDelete;

                        IDictionary <string, FieldDefinition> connectionFieldDefinitions = new Dictionary <string, FieldDefinition>();
                        if (connectionTypes != null)
                        {
                            foreach (var connectionType in connectionTypes)
                            {
                                if (connectionType.Name.Equals(connectionToAffect.ConnectionType))
                                {
                                    connectionFieldDefinitions = connectionType.Properties.FieldDefinitions;
                                    break;
                                }
                            }
                        }

                        // Connection assets returned from the cloud have null values for their encrypted fields,
                        // so keep the old local asset encrypted field values instead of overwriting the local asset encrypted field values with null
                        foreach (var valueField in connectionToDelete.ValueFields)
                        {
                            if (connectionToAffect.getFields()[valueField.Key] == null && connectionFieldDefinitions[valueField.Key].IsEncrypted)
                            {
                                connectionToAffect.getFields()[valueField.Key] = valueField.Value;
                            }
                        }

                        localAssets.Connections.Remove(connectionToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Connections.Add(new ConnectionJson(connectionToAffect));
                    }
                }
                else if (assetToAffect is AutomationCertificate)
                {
                    var certToAffect = (AutomationCertificate)assetToAffect;

                    if (assetToDelete != null)
                    {
                        var certToDelete = (CertificateJson)assetToDelete;

                        // Certificate assets returned from the cloud have their passwords removed,
                        // so keep the old local asset password instead of overwriting the local asset password with null
                        if (certToAffect.getCertPath() == null)
                        {
                            certToAffect.setPassword(certToDelete.Password);
                            certToAffect.setCertPath(certToDelete.CertPath);
                        }

                        localAssets.Certificate.Remove(certToDelete);
                    }

                    if (replace)
                    {
                        localAssets.Certificate.Add(new CertificateJson(certToAffect));
                    }
                }
            }

            DirectoryInfo dir = Directory.CreateDirectory(workspacePath);

            UnsecureLocalAssetsContainerJson.Set(workspacePath, localAssets);
            SecureLocalAssetsContainerJson.Set(workspacePath, localAssets, encryptionCertThumbprint, connectionTypes);
        }
 public static void WriteSecureAssets(String workspacePath, LocalAssets assets, String encryptionCertThumbprint)
 {
     LocalAssetsStore.Write(workspacePath, assets, encryptionCertThumbprint);
 }
        /// <summary>
        /// Returns if the asset exists locally or in the cloud
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="assetType"></param>
        /// <param name="localWorkspacePath"></param>
        /// <param name="automationApi"></param>
        /// <param name="resourceGroupName"></param>
        /// <param name="automationAccountName"></param>
        /// <param name="encryptionCertThumbprint"></param>
        /// <returns>null if the asset does not exist or else returns the asset</returns>
        public static async Task <AutomationAsset> GetAsset(String assetName, String assetType, String localWorkspacePath, AutomationManagementClient automationApi, string resourceGroupName, string automationAccountName, string encryptionCertThumbprint, ICollection <ConnectionType> connectionTypes)
        {
            AutomationAsset automationAsset = null;

            // Get local assets
            LocalAssets localAssets = LocalAssetsStore.Get(localWorkspacePath, encryptionCertThumbprint, connectionTypes);

            // Search for variables
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.CancelAfter(TIMEOUT_MS);
            if (assetType == Constants.AssetType.Variable)
            {
                // Check local asset store first
                var localVariable = localAssets.Variables.Find(asset => asset.Name == assetName);
                if (localVariable != null)
                {
                    automationAsset = new AutomationVariable(localVariable);
                }
                else
                {
                    try
                    {
                        // Check cloud. Catch exception if it doesn't exist
                        VariableGetResponse cloudVariable = await automationApi.Variables.GetAsync(resourceGroupName, automationAccountName, assetName, cts.Token);

                        automationAsset = new AutomationVariable(cloudVariable.Variable);
                    }
                    catch (Exception e)
                    {
                        // If the exception is not found, don't throw new exception as this is expected
                        if (e.HResult != -2146233088)
                        {
                            throw e;
                        }
                    }
                }
            }
            // Search for credentials
            else if (assetType == Constants.AssetType.Credential)
            {
                // Check local asset store first
                var localCredential = localAssets.PSCredentials.Find(asset => asset.Name == assetName);
                if (localCredential != null)
                {
                    automationAsset = new AutomationCredential(localCredential);
                }
                else
                {
                    try
                    {
                        // Check cloud. Catch execption if it doesn't exist
                        CredentialGetResponse cloudVariable = await automationApi.PsCredentials.GetAsync(resourceGroupName, automationAccountName, assetName, cts.Token);

                        automationAsset = new AutomationCredential(cloudVariable.Credential);
                    }
                    catch (Exception e)
                    {
                        // If the exception is not found, don't throw new exception as this is expected
                        if (e.HResult != -2146233088)
                        {
                            throw e;
                        }
                    }
                }
            }
            // Search for connections
            else if (assetType == Constants.AssetType.Connection)
            {
                // Check local asset store first
                var localConnection = localAssets.Connections.Find(asset => asset.Name == assetName);
                if (localConnection != null)
                {
                    automationAsset = new AutomationConnection(localConnection);
                }
                else
                {
                    try
                    {
                        // Check cloud. Catch exception if it doesn't exist
                        ConnectionGetResponse cloudConnection = await automationApi.Connections.GetAsync(resourceGroupName, automationAccountName, assetName, cts.Token);

                        cts = new CancellationTokenSource();
                        cts.CancelAfter(TIMEOUT_MS);
                        ConnectionTypeGetResponse connectionType = await automationApi.ConnectionTypes.GetAsync(resourceGroupName, automationAccountName,
                                                                                                                cloudConnection.Connection.Properties.ConnectionType.Name, cts.Token);

                        automationAsset = new AutomationConnection(cloudConnection.Connection, connectionType.ConnectionType);
                    }
                    catch (Exception e)
                    {
                        // If the exception is not found, don't throw new exception as this is expected
                        if (e.HResult != -2146233088)
                        {
                            throw e;
                        }
                    }
                }
            }
            // Search for certificates
            else if (assetType == Constants.AssetType.Certificate)
            {
                // Check local asset store first
                var localCertificate = localAssets.Certificate.Find(asset => asset.Name == assetName);
                if (localCertificate != null)
                {
                    automationAsset = new AutomationCertificate(localCertificate);
                }
                else
                {
                    try
                    {
                        // Check cloud. Catch execption if it doesn't exist
                        CertificateGetResponse cloudCertificate = await automationApi.Certificates.GetAsync(resourceGroupName, automationAccountName, assetName, cts.Token);

                        automationAsset = new AutomationCertificate(cloudCertificate.Certificate);
                    }
                    catch (Exception e)
                    {
                        // If the exception is not found, don't throw new exception as this is expected
                        if (e.HResult != -2146233088)
                        {
                            throw e;
                        }
                    }
                }
            }
            return(automationAsset);
        }
        public static async Task <ISet <AutomationAsset> > GetAll(String localWorkspacePath, AutomationManagementClient automationApi, string resourceGroupName, string automationAccountName, string encryptionCertThumbprint, ICollection <ConnectionType> connectionTypes)
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.CancelAfter(TIMEOUT_MS);
            VariableListResponse cloudVariables = await automationApi.Variables.ListAsync(resourceGroupName, automationAccountName, cts.Token);

            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            CredentialListResponse cloudCredentials = await automationApi.PsCredentials.ListAsync(resourceGroupName, automationAccountName, cts.Token);

            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);
            ConnectionListResponse cloudConnections = await automationApi.Connections.ListAsync(resourceGroupName, automationAccountName, cts.Token);

            CertificateListResponse cloudCertificates = await automationApi.Certificates.ListAsync(resourceGroupName, automationAccountName, cts.Token);

            // need to get connections one at a time to get each connection's values. Values currently come back as empty in list call
            var connectionAssetsWithValues = new HashSet <Connection>();

            foreach (var connection in cloudConnections.Connection)
            {
                cts = new CancellationTokenSource();
                cts.CancelAfter(TIMEOUT_MS);
                var connectionResponse = await automationApi.Connections.GetAsync(resourceGroupName, automationAccountName, connection.Name, cts.Token);

                connectionAssetsWithValues.Add(connectionResponse.Connection);
            }

            LocalAssets localAssets = LocalAssetsStore.Get(localWorkspacePath, encryptionCertThumbprint, connectionTypes);

            var automationAssets = new SortedSet <AutomationAsset>();

            // Compare cloud variables to local
            foreach (var cloudAsset in cloudVariables.Variables)
            {
                var localAsset = localAssets.Variables.Find(asset => asset.Name == cloudAsset.Name);

                var automationAsset = (localAsset != null) ?
                                      new AutomationVariable(localAsset, cloudAsset) :
                                      new AutomationVariable(cloudAsset);

                automationAssets.Add(automationAsset);
            }

            // Add remaining locally created variables
            foreach (var localAsset in localAssets.Variables)
            {
                var automationAsset = new AutomationVariable(localAsset);
                automationAssets.Add(automationAsset);
            }

            // Compare cloud credentials to local
            foreach (var cloudAsset in cloudCredentials.Credentials)
            {
                var localAsset = localAssets.PSCredentials.Find(asset => asset.Name == cloudAsset.Name);

                var automationAsset = (localAsset != null) ?
                                      new AutomationCredential(localAsset, cloudAsset) :
                                      new AutomationCredential(cloudAsset);

                automationAssets.Add(automationAsset);
            }

            // Add remaining locally created credentials
            foreach (var localAsset in localAssets.PSCredentials)
            {
                var automationAsset = new AutomationCredential(localAsset);
                automationAssets.Add(automationAsset);
            }

            // Compare cloud connections to local
            foreach (var cloudAsset in connectionAssetsWithValues)
            {
                ConnectionTypeGetResponse connectionType = await automationApi.ConnectionTypes.GetAsync(resourceGroupName, automationAccountName, cloudAsset.Properties.ConnectionType.Name);

                var localAsset = localAssets.Connections.Find(asset => asset.Name == cloudAsset.Name);

                var automationAsset = (localAsset != null) ?
                                      new AutomationConnection(localAsset, cloudAsset) :
                                      new AutomationConnection(cloudAsset, connectionType.ConnectionType);

                automationAssets.Add(automationAsset);
            }

            // Add remaining locally created connections
            foreach (var localAsset in localAssets.Connections)
            {
                var automationAsset = new AutomationConnection(localAsset);
                automationAssets.Add(automationAsset);
            }

            // Compare cloud certificates to local
            foreach (var cloudAsset in cloudCertificates.Certificates)
            {
                var localAsset = localAssets.Certificate.Find(asset => asset.Name == cloudAsset.Name);

                var automationAsset = (localAsset != null) ?
                                      new AutomationCertificate(localAsset, cloudAsset) :
                                      new AutomationCertificate(cloudAsset);

                automationAssets.Add(automationAsset);
            }

            // Add remaining locally created certificates
            foreach (var localAsset in localAssets.Certificate)
            {
                var automationAsset = new AutomationCertificate(localAsset);
                automationAssets.Add(automationAsset);
            }

            return(automationAssets);
        }
 public static void SaveLocally(String localWorkspacePath, ICollection <AutomationAsset> assets, String encryptionCertThumbprint, ICollection <ConnectionType> connectionTypes)
 {
     LocalAssetsStore.Add(localWorkspacePath, assets, encryptionCertThumbprint, connectionTypes);
 }
        public static async Task <ISet <AutomationAsset> > GetAll(String localWorkspacePath, AutomationManagementClient automationApi, string resourceGroupName, string automationAccountName, string encryptionCertThumbprint)
        {
            VariableListResponse cloudVariables = await automationApi.Variables.ListAsync(resourceGroupName, automationAccountName);

            CredentialListResponse cloudCredentials = await automationApi.PsCredentials.ListAsync(resourceGroupName, automationAccountName);

            // TODO: need to get one at a time to get values. values currently comes back as empty
            //ConnectionListResponse cloudConnections = await automationApi.Connections.ListAsync(resourceGroupName, automationAccountName);

            LocalAssets localAssets = LocalAssetsStore.Get(localWorkspacePath, encryptionCertThumbprint);

            var automationAssets = new SortedSet <AutomationAsset>();

            // Compare cloud variables to local
            foreach (var cloudAsset in cloudVariables.Variables)
            {
                var localAsset = localAssets.Variables.Find(asset => asset.Name == cloudAsset.Name);

                var automationAsset = (localAsset != null) ?
                                      new AutomationVariable(localAsset, cloudAsset) :
                                      new AutomationVariable(cloudAsset);

                automationAssets.Add(automationAsset);
            }

            // Add remaining locally created variables
            foreach (var localAsset in localAssets.Variables)
            {
                var automationAsset = new AutomationVariable(localAsset);
                automationAssets.Add(automationAsset);
            }

            // Compare cloud credentials to local
            foreach (var cloudAsset in cloudCredentials.Credentials)
            {
                var localAsset = localAssets.PSCredentials.Find(asset => asset.Name == cloudAsset.Name);

                var automationAsset = (localAsset != null) ?
                                      new AutomationCredential(localAsset, cloudAsset) :
                                      new AutomationCredential(cloudAsset);

                automationAssets.Add(automationAsset);
            }

            // Add remaining locally created credentials
            foreach (var localAsset in localAssets.PSCredentials)
            {
                var automationAsset = new AutomationCredential(localAsset);
                automationAssets.Add(automationAsset);
            }

            // Compare cloud connections to local

            /*foreach (var cloudAsset in cloudConnections.Connection)
             * {
             *  var localAsset = localAssets.Connections.Find(asset => asset.Name == cloudAsset.Name);
             *
             *  var automationAsset = (localAsset != null) ?
             *          new AutomationConnection(localAsset, cloudAsset) :
             *          new AutomationConnection(cloudAsset);
             *
             *  automationAssets.Add(automationAsset);
             * }
             *
             * // Add remaining locally created connections
             * foreach (var localAsset in localAssets.Connections)
             * {
             *  var automationAsset = new AutomationConnection(localAsset);
             *  automationAssets.Add(automationAsset);
             * }*/

            return(automationAssets);
        }
Пример #11
0
 public static void SetLocalEncryptedAssets(String localWorkspacePath, LocalAssets locaAssets, string encryptionCertThumbprint)
 {
     LocalAssetsStore.WriteSecureAssets(localWorkspacePath, locaAssets, encryptionCertThumbprint);
 }
Пример #12
0
        public static LocalAssets GetLocalEncryptedAssets(String localWorkspacePath, string encryptionCertThumbprint)
        {
            LocalAssets localAssets = LocalAssetsStore.Get(localWorkspacePath, encryptionCertThumbprint, null);

            return(localAssets);
        }