예제 #1
0
        private async Task <StringEntity> GetNotifiedAccountEntity(IdentifiedConnectionString connStr, CancellationToken cancellationToken)
        {
            var retrieveOp       = TableOperation.Retrieve <StringEntity>(GetPartitionKey(connStr), GetRowKey(connStr));
            var retrieveResponse = await this.notifiedAccountsTable.ExecuteAsync(retrieveOp, cancellationToken);

            return(retrieveResponse.Result as StringEntity);
        }
예제 #2
0
        private Task InsertNotifiedAccountEntity(IdentifiedConnectionString connStr, CancellationToken cancellationToken)
        {
            var entity = new StringEntity
            {
                PartitionKey = GetPartitionKey(connStr),
                RowKey       = GetRowKey(connStr),
                Value        = JsonConvert.SerializeObject(connStr),
            };

            var insertOp = TableOperation.Insert(entity);

            return(this.notifiedAccountsTable.ExecuteAsync(insertOp, cancellationToken));
        }
예제 #3
0
        private async Task <string> FindGitHubEmail(IdentifiedConnectionString connStr, CancellationToken cancellationToken)
        {
            var githubClient = new GitHubClient(new ProductHeaderValue("CodeSearch.Notifier"));

            githubClient.Credentials = new Credentials(Environment.GetEnvironmentVariable(GitHubApiKeyEnvVarName));

            var repoParts = connStr.Repository.Split('/');

            if (repoParts.Length != 2)
            {
                return(null);
            }

            var githubLogin = repoParts[0];
            var user        = await githubClient.User.Get(githubLogin);

            if (user == null)
            {
                return(null);
            }

            int page = 1;

            while (true)
            {
                string url    = string.Format("https://api.github.com/user/{0}/events?page={1}", user.Id, page);
                var    events = await githubClient.Connection.Get <IEnumerable <Activity> >(new Uri(url), new Dictionary <string, string>(0), "application/json", cancellationToken);

                if (events.Body.Any() == false)
                {
                    break;
                }

                var pushEvent = events.Body.FirstOrDefault(e => "PushEvent".Equals(e.Type, StringComparison.OrdinalIgnoreCase));
                if (pushEvent != null)
                {
                    var pushPayload = pushEvent.Payload as PushEventPayload;
                    if (pushPayload != null && pushPayload.Commits != null && pushPayload.Commits.Count > 0)
                    {
                        var commit = pushPayload.Commits.FirstOrDefault(c => c.Author != null && string.IsNullOrWhiteSpace(c.Author.Email) == false);
                        return(commit.Author.Email);
                    }
                }

                page++;
            }

            return(string.Empty);
        }
예제 #4
0
        private async Task SendEmail(string toEmail, IdentifiedConnectionString connStr, CancellationToken cancellationToken)
        {
            var mailgunDomain = Environment.GetEnvironmentVariable(MailGunDomainEnvVarName);
            var mailgunApiKey = Environment.GetEnvironmentVariable(MailGunApiKeyEnvVarName);

            var cloudAccount = CloudStorageAccount.Parse(connStr.ConnectionString);
            var subject      = string.Format("Warning: Your Azure storage account ({0}) might be exposed", cloudAccount.Credentials.AccountName);
            var body         = new StringBuilder();

            body.AppendLine("Hi there!");
            body.AppendLine();
            body.AppendLine("Your Azure storage credentials are publicly visible from GitHub: ");
            body.AppendLine(string.Format(" - GitHub repository: {0}", connStr.Repository));
            body.AppendLine(string.Format(" - Storage account: {0}", cloudAccount.Credentials.AccountName));
            body.AppendLine(string.Format(" - Storage key: {0}...", cloudAccount.Credentials.ExportBase64EncodedKey().Substring(0, 7)));
            body.AppendLine();
            body.AppendLine("Don't forget to update your application to the alternate key (primary to seconadry and vice versa) before re-generating the keys!");
            body.AppendLine();
            body.AppendLine("Here's a blog post from Scott Hanselman on how to keep the connection string in Azure: http://bit.ly/1NCcAN6");
            body.AppendLine("Let me know if you want to unsubscribe from these messages, or just need help with connection string stuff in general.");
            body.AppendLine("You can respond to this email or contact me on twitter @sluu99.");
            body.AppendLine();
            body.AppendLine("Good luck!");
            body.AppendLine("- Steven");
            body.AppendLine();
            body.AppendLine("Disclaimer: This is a personal project and is not associated with Microsoft nor Microsoft Azure.");

            var credentials = new NetworkCredential("api", mailgunApiKey);
            var postBody    = new Dictionary <string, string>
            {
                { "from", "*****@*****.**" },
                { "to", toEmail },
                { "bcc", "*****@*****.**" },
                { "subject", subject },
                { "text", body.ToString() },
            };
            string url = string.Format("https://api.mailgun.net/v3/{0}/messages", mailgunDomain);

            using (var httpHandler = new HttpClientHandler {
                Credentials = credentials
            })
                using (var httpClient = new HttpClient(httpHandler))
                    using (var postContent = new FormUrlEncodedContent(postBody))
                        using (var postResponse = await httpClient.PostAsync(url, postContent, cancellationToken))
                        {
                        }

            Trace.TraceInformation("Email sent to {0}: {1}", toEmail, subject);
        }
예제 #5
0
        private async Task<string> FindGitHubEmail(IdentifiedConnectionString connStr, CancellationToken cancellationToken)
        {
            var githubClient = new GitHubClient(new ProductHeaderValue("CodeSearch.Notifier"));
            githubClient.Credentials = new Credentials(Environment.GetEnvironmentVariable(GitHubApiKeyEnvVarName));

            var repoParts = connStr.Repository.Split('/');
            if (repoParts.Length != 2)
            {
                return null;
            }

            var githubLogin = repoParts[0];
            var user = await githubClient.User.Get(githubLogin);
            if (user == null)
            {
                return null;
            }

            int page = 1;
            while (true)
            {
                string url = string.Format("https://api.github.com/user/{0}/events?page={1}", user.Id, page);
                var events = await githubClient.Connection.Get<IEnumerable<Activity>>(new Uri(url), new Dictionary<string, string>(0), "application/json", cancellationToken);
                
                if (events.Body.Any() == false)
                {
                    break;
                }

                var pushEvent = events.Body.FirstOrDefault(e => "PushEvent".Equals(e.Type, StringComparison.OrdinalIgnoreCase));
                if (pushEvent != null)
                {
                    var pushPayload = pushEvent.Payload as PushEventPayload;
                    if (pushPayload != null && pushPayload.Commits != null && pushPayload.Commits.Count > 0)
                    {
                        var commit = pushPayload.Commits.FirstOrDefault(c => c.Author != null && string.IsNullOrWhiteSpace(c.Author.Email) == false);
                        return commit.Author.Email;
                    }
                }

                page++;
            }

            return string.Empty;
        }
예제 #6
0
 private static string GetRowKey(IdentifiedConnectionString connStr)
 {
     return Sha1(connStr.ConnectionString.ToLowerInvariant());
 }
예제 #7
0
 private static string GetPartitionKey(IdentifiedConnectionString connStr)
 {
     return Sha1(connStr.Repository.ToLowerInvariant());
 }
예제 #8
0
        private async Task<StringEntity> GetNotifiedAccountEntity(IdentifiedConnectionString connStr, CancellationToken cancellationToken)
        {
            var retrieveOp = TableOperation.Retrieve<StringEntity>(GetPartitionKey(connStr), GetRowKey(connStr));
            var retrieveResponse = await this.notifiedAccountsTable.ExecuteAsync(retrieveOp, cancellationToken);

            return retrieveResponse.Result as StringEntity;
        }
예제 #9
0
        private Task InsertNotifiedAccountEntity(IdentifiedConnectionString connStr, CancellationToken cancellationToken)
        {
            var entity = new StringEntity
            {
                PartitionKey = GetPartitionKey(connStr),
                RowKey = GetRowKey(connStr),
                Value = JsonConvert.SerializeObject(connStr),
            };

            var insertOp = TableOperation.Insert(entity);
            return this.notifiedAccountsTable.ExecuteAsync(insertOp, cancellationToken);
        }
예제 #10
0
        private async Task SendEmail(string toEmail, IdentifiedConnectionString connStr, CancellationToken cancellationToken)
        {
            var mailgunDomain = Environment.GetEnvironmentVariable(MailGunDomainEnvVarName);
            var mailgunApiKey = Environment.GetEnvironmentVariable(MailGunApiKeyEnvVarName);

            var cloudAccount = CloudStorageAccount.Parse(connStr.ConnectionString);
            var subject = string.Format("Warning: Your Azure storage account ({0}) might be exposed", cloudAccount.Credentials.AccountName);
            var body = new StringBuilder();
            body.AppendLine("Hi there!");
            body.AppendLine();
            body.AppendLine("Your Azure storage credentials are publicly visible from GitHub: ");
            body.AppendLine(string.Format(" - GitHub repository: {0}", connStr.Repository));
            body.AppendLine(string.Format(" - Storage account: {0}", cloudAccount.Credentials.AccountName));
            body.AppendLine(string.Format(" - Storage key: {0}...", cloudAccount.Credentials.ExportBase64EncodedKey().Substring(0, 7)));
            body.AppendLine();
            body.AppendLine("Don't forget to update your application to the alternate key (primary to seconadry and vice versa) before re-generating the keys!");
            body.AppendLine();
            body.AppendLine("Here's a blog post from Scott Hanselman on how to keep the connection string in Azure: http://bit.ly/1NCcAN6");
            body.AppendLine("Let me know if you want to unsubscribe from these messages, or just need help with connection string stuff in general.");
            body.AppendLine("You can respond to this email or contact me on twitter @sluu99.");
            body.AppendLine();
            body.AppendLine("Good luck!");
            body.AppendLine("- Steven");
            body.AppendLine();
            body.AppendLine("Disclaimer: This is a personal project and is not associated with Microsoft nor Microsoft Azure.");

            var credentials = new NetworkCredential("api", mailgunApiKey);
            var postBody = new Dictionary<string, string>
            {
                { "from", "*****@*****.**" },
                { "to", toEmail },
                { "bcc", "*****@*****.**" },
                { "subject", subject },
                { "text", body.ToString() },
            };
            string url = string.Format("https://api.mailgun.net/v3/{0}/messages", mailgunDomain);

            using (var httpHandler = new HttpClientHandler { Credentials = credentials })
            using (var httpClient = new HttpClient(httpHandler))
            using (var postContent = new FormUrlEncodedContent(postBody))
            using (var postResponse = await httpClient.PostAsync(url, postContent, cancellationToken))
            {                
            }

            Trace.TraceInformation("Email sent to {0}: {1}", toEmail, subject);
        }
예제 #11
0
 private static string GetRowKey(IdentifiedConnectionString connStr)
 {
     return(Sha1(connStr.ConnectionString.ToLowerInvariant()));
 }
예제 #12
0
 private static string GetPartitionKey(IdentifiedConnectionString connStr)
 {
     return(Sha1(connStr.Repository.ToLowerInvariant()));
 }