Esempio n. 1
0
        // N/A

        #endregion

        #region Delete

        public void Delete(IPSpec ipSpec)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repo = context.GetRepository <IPSpec>();

                repo.Delete(ipSpec);
            }
        }
Esempio n. 2
0
        /*
         * Create a new IPSpec object using the passed address.
         */
        public static IPSpec Create(string address)
        {
            IPSpec ipSpec;

            try
            {
                ipSpec = new IPSpec(address);

                IPSpecDC.Create(ipSpec);
            }
            catch (Exception ex)
            {
                return(null);
            }

            return(ipSpec);
        }
Esempio n. 3
0
        public HttpResponseMessage Create(string name, string ip)
        {
            IPSpec ipSpec = null;

            try
            {
                ipSpec = IPSpecManager.Create(name, ip);
            }
            catch (IPSpecExistsException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Conflict, ex.Message));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.Created, ipSpec));
        }
        public HttpResponseMessage Delete(int id)
        {
            IPSpec ipSpec = IPSpecManager.GetById(id);

            if (ipSpec == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "IP spec not found."));
            }

            try
            {
                IPSpecManager.Delete(ipSpec);
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed to delete IP spec."));
            }

            return(Request.CreateResponse(HttpStatusCode.NoContent));
        }
Esempio n. 5
0
        /*
         * Check to see if the passed address is whitelisted.
         */
        public static bool IsWhitelisted(string address)
        {
            if (!IPAddress.TryParse(address, out _))
            {
                // see if address is an IP plus port, e.g. "1.1.1.1:58290"
                if (Uri.TryCreate(Uri.UriSchemeHttps + Uri.SchemeDelimiter + address, UriKind.Absolute, out Uri uri))
                {
                    if (uri.HostNameType != UriHostNameType.IPv4 && uri.HostNameType != UriHostNameType.IPv6)
                    {
                        return(false);
                    }

                    address = uri.Host;
                }
            }

            IPSpec ipSpec = IPSpecDC.FindByAddress(address);

            return(ipSpec != null);
        }
Esempio n. 6
0
        /// <summary>
        /// Create a new IPSpec object using the passed name and address.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public static IPSpec Create(string name, string address)
        {
            IPSpec ipSpec = IPSpecDC.GetByName(name);

            if (ipSpec != null)
            {
                throw new IPSpecExistsException($"An entry named '{ipSpec.Name}' already exists.");
            }

            ipSpec = IPSpecDC.Get(address);

            if (ipSpec != null)
            {
                throw new IPSpecExistsException($"IP '{address}' is already whitelisted by entry named '{ipSpec.Name}'.");
            }

            ipSpec = new IPSpec(name, address);

            IPSpecDC.Create(ipSpec);

            return(ipSpec);
        }
        /// <summary>
        /// Upgrades to 00.09.01
        ///
        /// Operations:
        /// - Generate a Salt.
        /// - Hash existing Address' using the new Salt.
        /// - Insert in to new table.
        /// </summary>
        private void Upgrade_00_09_01()
        {
            string oldTableName = "{databaseOwner}[{objectQualifier}Cantarus_PolyDeploy_IPSpecs_PreEncryption]";
            string newTableName = "{databaseOwner}[{objectQualifier}Cantarus_PolyDeploy_IPSpecs]";

            using (IDataContext context = DataContext.Instance())
            {
                // Get all existing IPSpec ids.
                IEnumerable <int> ipSpecIds = context.ExecuteQuery <int>(System.Data.CommandType.Text, $"SELECT [IPSpecID] FROM {oldTableName}");

                foreach (int ipSpecId in ipSpecIds)
                {
                    // Read old data.
                    string isAddress = context.ExecuteQuery <string>(
                        System.Data.CommandType.Text,
                        $"SELECT [Address] FROM {oldTableName} WHERE IPSpecID = @0",
                        ipSpecId
                        ).FirstOrDefault();

                    // Create a name.
                    string isName = $"Unnamed#{ipSpecId}";

                    // Generate a salt.
                    string isSalt = IPSpec.GenerateSalt();

                    // Use existing plain text address and salt to create a hashed address.
                    string isAddressSha = IPSpec.GenerateHash(isAddress, isSalt);

                    // Insert in to new table.
                    string insertSql = $"SET IDENTITY_INSERT {newTableName} ON;"
                                       + $"INSERT INTO {newTableName} ([IPSpecID], [Name], [Address_Sha], [Salt])"
                                       + $"VALUES (@0, @1, @2, @3);"
                                       + $"SET IDENTITY_INSERT {newTableName} OFF;";

                    context.Execute(System.Data.CommandType.Text, insertSql, ipSpecId, isName, isAddressSha, isSalt);
                }
            }
        }
        public HttpResponseMessage Create(string ip)
        {
            IPSpec ipSpec = IPSpecManager.Create(ip);

            return(Request.CreateResponse(HttpStatusCode.Created, ipSpec));
        }
Esempio n. 9
0
 /*
  * Delete the passed IPSpec.
  */
 public static void Delete(IPSpec ipSpec)
 {
     IPSpecDC.Delete(ipSpec);
 }
Esempio n. 10
0
        /*
         * Check to see if the passed address is whitelisted.
         */
        public static bool IsWhitelisted(string address)
        {
            IPSpec ipSpec = IPSpecDC.FindByAddress(address);

            return(ipSpec != null);
        }