//методы public virtual AmazonCloudFrontClient GetCloudFrontClient() { RegionEndpoint endpoint = RegionEndpoint.GetBySystemName(Settings.RegionEndpoint); bool isUnknownRegion = AmazonConstansts.UNKNOWN_CREDENTIALS_REGION.Equals( endpoint.DisplayName, StringComparison.InvariantCultureIgnoreCase); if (isUnknownRegion) { string comment = string.Format("Amazon region с именем {0} не обнаружен.", endpoint); Exception exception = new KeyNotFoundException(comment); _logger.Exception(exception); throw exception; } var client = new AmazonCloudFrontClient(Settings.AccessKey, Settings.SecretKey, endpoint); return client; }
protected virtual AmazonCloudFront GetClient() { AmazonCredentials.KeyPair credentials = base.GetCredentials(); AmazonCloudFront client = new AmazonCloudFrontClient(credentials.AwsAccessKeyId, credentials.AwsSecretAccessKey); return client; }
public static void Main(string [] args) { bool done = false; string result, cfDate, cfVersion = "2010-08-01", cfContent, cfKey1 = "AKIAJARKKNFC2QKIOQFA", cfKey2 = "qMlOAQioqwN0ho0XyDOYfl/V1GXEivZHYPyo//Qu", cfSign; ThreeSharpWrapper s3 = new ThreeSharpWrapper(cfKey1, cfKey2); SecureString cfSecret = new SecureString(); s3.config.IsSecure = false; Console.Write("Upload " + args [0] + "? Y/N"); if ("y".Equals(Console.ReadLine(), StringComparison.InvariantCultureIgnoreCase)) { while (!done) { try { Console.Write("Uploading..."); s3.AddFileObject("roxority", args [0] + ".zip", @"c:\s3sync\" + args [0] + ".zip"); Console.WriteLine("Done."); done = true; } catch (Exception ex) { Console.WriteLine("\r\n" + ex.Message + " ---retrying."); done = false; } } } done = false; while (!done) { try { Console.Write("Setting permissions..."); using (ACLChangeRequest request = new ACLChangeRequest("roxority", args [0] + ".zip?acl", "public-read")) s3.service.ACLChange(request).Dispose(); Console.WriteLine("Done."); done = true; } catch (Exception ex) { Console.WriteLine("\r\n" + ex.Message + " ---retrying."); done = false; } } done = false; cfDate = AmazonCloudFrontUtil.FormattedCurrentTimestamp; cfContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><InvalidationBatch xmlns=\"http://cloudfront.amazonaws.com/doc/" + cfVersion + "/\"><Path>/" + args [0] + ".zip</Path><CallerReference>" + DateTime.Now.Ticks + "</CallerReference></InvalidationBatch>"; foreach (char c in cfKey2) { cfSecret.AppendChar(c); } using (AmazonCloudFrontClient cfc = new Amazon.CloudFront.AmazonCloudFrontClient(cfKey1, cfSecret, new AmazonCloudFrontConfig())) using (WebClient wc = new WebClient()) while (!done) { try { Console.Write("CloudFront invalidation..."); wc.Headers ["x-amz-date"] = cfDate; wc.Headers [HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=utf-8"; using (HMACSHA1 sha1 = new HMACSHA1()) cfSign = AWSSDKUtils.HMACSign(cfDate, cfSecret, sha1); wc.Headers [HttpRequestHeader.Authorization] = "AWS " + cfKey1 + ":" + cfSign; if (string.IsNullOrEmpty(result = wc.UploadString("https://cloudfront.amazonaws.com/" + cfVersion + "/distribution/E14JHINLXE5EL1/invalidation", cfContent)) || !result.Contains("<Status>InProgress</Status>")) { throw new Exception("\r\n" + result + "\r\n"); } Console.WriteLine("Done."); done = true; } catch (Exception ex) { Console.WriteLine("\r\n" + ex.Message + " ---retrying."); done = false; } } Console.Beep(); Console.WriteLine("DONE"); Console.ReadLine(); }
public async Task MapBinaryRoutesInCloudFrontAsync(string beanstalkUrl, string subdomain, string deploymentEnvironmentName) { var distributionResponse = await GetDistributionByCNAMEAsync(); var domainHost = GetHostFromUrl(beanstalkUrl); if (distributionResponse.Distribution == null) { loggerProvider.GetLogger().Debug($"No action being taken, could not find distribution associated with CNAME. [CNAME: {cfConfigurationProvider.CNAME}]"); return; } bool originAdded = false; var origin = distributionResponse.Distribution.DistributionConfig.Origins.Items.FirstOrDefault(o => o.DomainName == domainHost); if (origin == null) { loggerProvider.GetLogger().Debug($"Origin not found. Creating new origin. [DomainName: {domainHost}]"); origin = new Origin { Id = $"{Capitalize(subdomain)} API {Capitalize(deploymentEnvironmentName)} Beanstalk", DomainName = domainHost, OriginPath = string.Empty, CustomOriginConfig = new CustomOriginConfig { HTTPPort = 80, HTTPSPort = 443, OriginProtocolPolicy = new OriginProtocolPolicy("https-only"), OriginSslProtocols = new OriginSslProtocols { Items = new List<string> { "TLSv1", "TLSv1.1", "TLSv1.2" }, Quantity = 3 } }, CustomHeaders = new CustomHeaders { Quantity = 1, Items = new List<OriginCustomHeader> { new OriginCustomHeader { HeaderName = "x-mat-gateway-secret", HeaderValue = cfConfigurationProvider.MaterialGatewaySecret} } } }; distributionResponse.Distribution.DistributionConfig.Origins.Items.Add(origin); distributionResponse.Distribution.DistributionConfig.Origins.Quantity++; originAdded = true; } bool cacheBehaviorAdded = false; var routes = GetListOfBinaryRoutesAsync(); foreach(var route in routes) { var cacheBehavior = distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Items.FirstOrDefault(behavior => behavior.TargetOriginId == origin.Id && behavior.PathPattern.IsEqualIgnoreCase(route)); if(cacheBehavior == null) { loggerProvider.GetLogger().Debug($"Cache Behavior not found. Creating new Cache behavior. [OriginId: {origin.Id}] [Route: {route}]"); cacheBehavior = new CacheBehavior { TargetOriginId = origin.Id, PathPattern = route, AllowedMethods = new AllowedMethods { Items = new List<string> {"GET","HEAD","POST","PUT","PATCH","OPTIONS","DELETE"}, Quantity = 7, CachedMethods = new CachedMethods { Items = new List<string> { "GET", "HEAD", "OPTIONS" }, Quantity = 3 } }, ForwardedValues = new ForwardedValues { QueryString = true, Cookies = new CookiePreference { Forward = new ItemSelection("none"), WhitelistedNames = new CookieNames { Quantity = 0 } }, Headers = new Headers { Items = new List<string> {"x-vol-tenant", "x-vol-test", "accept", "authorization"}, Quantity = 4 } }, ViewerProtocolPolicy = new ViewerProtocolPolicy("redirect-to-https"), TrustedSigners = new TrustedSigners { Enabled = false, Quantity = 0 }, MinTTL = 0, MaxTTL = 0, DefaultTTL = 0, Compress = true, SmoothStreaming = false }; distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Items.Add(cacheBehavior); distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Quantity++; cacheBehaviorAdded = true; } } loggerProvider.GetLogger().Debug("Removing CacheBehavior routes that are no longer listed in the binary routes path."); var originalList = distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Items; distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Items = originalList.Where(behavior => behavior.TargetOriginId != origin.Id || (behavior.TargetOriginId == origin.Id && routes.Contains(behavior.PathPattern))).ToList(); var itemsRemoved = originalList.Count != distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Items.Count; if (cacheBehaviorAdded || originAdded || itemsRemoved) { loggerProvider.GetLogger().Debug($"New Origin or Cache Behavior. Updating CloudFront instance. [CacheBehaviorAdded: {cacheBehaviorAdded}] [OriginAdded: {originAdded}] [ItemsRemoved: {itemsRemoved}]"); loggerProvider.GetLogger().Debug("Sorting order of cache behaviors."); //verify that new cacheBehaviors are ahead of their API Gateway cache behaviors for order of precendent issues!! distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Items = new List<CacheBehavior>(distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Items.OrderByDescending(behavior => behavior.PathPattern)); distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Quantity = distributionResponse.Distribution.DistributionConfig.CacheBehaviors.Items.Count; //update distribution using (var client = new AmazonCloudFrontClient(cfConfigurationProvider.AccessKey, cfConfigurationProvider.SecretKey, cfConfigurationProvider.RegionEndpoint)) { var response = await client.UpdateDistributionAsync(new UpdateDistributionRequest { DistributionConfig = distributionResponse.Distribution.DistributionConfig, Id = distributionResponse.Distribution.Id, IfMatch = distributionResponse.ETag }); await WaitUntilDistributionIsDeployedAsync(response.Distribution); loggerProvider.GetLogger().Debug("Finished updating/verifying the cloudfront distribution."); } } }
/// <summary> /// /// </summary> /// <param name="distribution"></param> /// <returns></returns> private async Task WaitUntilDistributionIsDeployedAsync(Distribution distribution) { const string DEPLOYED_STATUS = "deployed"; using (var client = new AmazonCloudFrontClient(cfConfigurationProvider.AccessKey, cfConfigurationProvider.SecretKey, cfConfigurationProvider.RegionEndpoint)) { loggerProvider.GetLogger().Debug($"Waiting for CloudFront to deploy. [Cname: {cfConfigurationProvider.CNAME}] [Status: {distribution.Status}]"); var currentStatus = distribution.Status.ToLower(); do { await Task.Delay(15000); var getDistributionResponse = await client.GetDistributionAsync(new GetDistributionRequest(distribution.Id)); currentStatus = getDistributionResponse.Distribution.Status; loggerProvider.GetLogger().Debug($"Checking CloudFront status. [Cname: {cfConfigurationProvider.CNAME}] [Current Status: {currentStatus}]"); } while (!currentStatus.IsEqualIgnoreCase(DEPLOYED_STATUS)); } }
private async Task<CloudFrontSetting> CreateCloudFrontDistributionAsync(string bucketName, bool shouldCache = true) { using ( var client = new AmazonCloudFrontClient(cfConfigurationProvider.AccessKey, cfConfigurationProvider.SecretKey, cfConfigurationProvider.RegionEndpoint)) { var originId = $"S3-{bucketName}"; var distrubtions = await client.ListDistributionsAsync(); var existingDist = distrubtions.DistributionList.Items.SingleOrDefault( summary => summary.Origins.Items.Any(origin => origin.Id == originId)); if (existingDist != null) { return new CloudFrontSetting {BucketName = bucketName, DomainName = existingDist.DomainName}; } var distributionConfig = new DistributionConfig { CallerReference = bucketName, Enabled = true, Comment = string.Empty, Origins = new Origins { Items = { new Origin { Id = originId, DomainName = string.Format("{0}.s3.amazonaws.com", bucketName), S3OriginConfig = new S3OriginConfig {OriginAccessIdentity = string.Empty} } }, Quantity = 1 }, DefaultCacheBehavior = new DefaultCacheBehavior { AllowedMethods = new AllowedMethods {Items = {"HEAD", "GET"}, Quantity = 2}, ForwardedValues = new ForwardedValues { Headers = new Headers { Items = { "Access-Control-Request-Headers", "Access-Control-Request-Method", "Origin" }, Quantity = 3 }, QueryString = true, Cookies = new CookiePreference {Forward = new ItemSelection("none")} }, TrustedSigners = new TrustedSigners {Enabled = false, Quantity = 0}, MinTTL = 0, MaxTTL = 31536000, DefaultTTL = shouldCache ? 86400 : 0, TargetOriginId = string.Format("S3-{0}", bucketName), ViewerProtocolPolicy = new ViewerProtocolPolicy("allow-all"), }, PriceClass = PriceClass.PriceClass_All }; var request = new CreateDistributionRequest(distributionConfig); var response = await client.CreateDistributionAsync(request); return new CloudFrontSetting { BucketName = response.Distribution.DistributionConfig.CallerReference, DomainName = response.Distribution.DomainName }; } }
/// <summary> /// Checks if Cloudfront with CNAME already exists. Returns true if it exist, otherwise false. /// </summary> /// <returns></returns> private async Task<GetDistributionResponse> GetDistributionByCNAMEAsync() { loggerProvider.GetLogger().Debug($"Checking if cloudfront exists. [CNAME: {cfConfigurationProvider.CNAME}]"); using (var client = new AmazonCloudFrontClient(cfConfigurationProvider.AccessKey, cfConfigurationProvider.SecretKey, cfConfigurationProvider.RegionEndpoint)) { var listDistributionResponse = await client.ListDistributionsAsync(); var distributionSummary = listDistributionResponse.DistributionList.Items.SingleOrDefault(cf => cf.Aliases.Items.Any(alias => alias.ToLower().StartsWith(cfConfigurationProvider.CNAME.ToLower()))); if (distributionSummary == null) { return null; } else { var distributionResponse = await client.GetDistributionAsync(new GetDistributionRequest(distributionSummary.Id)); return distributionResponse; } } }
static void Main() { var distributionId = "E24L1FD4ZU4BTF";//"EJCDLYDTUKDBE"; var client = new AmazonCloudFrontClient(); var batch = new InvalidationBatch() .WithCallerReference(Guid.NewGuid().ToString()) .WithPaths(new Paths() .WithQuantity(1) .WithItems("/account/logon")); var request = new CreateInvalidationRequest() .WithDistributionId(distributionId) .WithInvalidationBatch(batch); var response = client.CreateInvalidation(request); var invalidationId = response.CreateInvalidationResult.Invalidation.Id; if (string.IsNullOrEmpty(invalidationId)) return; while (true) { var invalidationResponse = client.GetInvalidation( new GetInvalidationRequest().WithDistributionId(distributionId).WithId(invalidationId)); var status = invalidationResponse.GetInvalidationResult.Invalidation.Status; if (status != "InProgress") break; Console.WriteLine("Waiting 10secs before checking..."); Thread.Sleep(10000); } Console.WriteLine("Invalidation complete!"); }