Skip to content

Commit

Permalink
Add .env file for user credentials not public
Browse files Browse the repository at this point in the history
  • Loading branch information
hros18 committed Apr 29, 2021
1 parent a868716 commit 4630258
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AWS_BUCKET_NAME=
AWS_BUCKET_PUBLIC_KEY=
AWS_BUCKET_SECRET_KEY=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,5 @@ MigrationBackup/
.DS_Store

src/Blogifier/Blog.db

.env
33 changes: 30 additions & 3 deletions src/Blogifier.Core/Providers/BucketProvider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using System.Reflection;
using Blogifier.Core.Extensions;
using Blogifier.Shared;
Expand Down Expand Up @@ -36,8 +37,9 @@ public BucketProvider(IConfiguration configuration)
{
_configuration = configuration;
_storageRoot = $"{ContentRoot}{_slash}wwwroot{_slash}data{_slash}";
publicKey = _configuration["AWSCredentials:S3Bucket:accessKey"];
secretKey = _configuration["AWSCredentials:S3Bucket:secretKey"];
var tuple = GetAWSCredentials();
publicKey = tuple.Item1;
secretKey = tuple.Item2;
s3Client = new AmazonS3Client(publicKey, secretKey, bucketRegion);
}

Expand All @@ -46,6 +48,30 @@ public bool FileExists(string path)
throw new NotImplementedException();
}

private (string, string) GetAWSCredentials() {
var filePath = "../../.env";
var publicKeyS = "";
var secretKeyS = "";


if (!File.Exists(filePath)) {
return (null, null);
}

foreach (var line in File.ReadAllLines(filePath))
{
var parts = line.Split('=',StringSplitOptions.RemoveEmptyEntries);

if (parts.Length != 2)
continue;

if(parts[0] == "AWS_BUCKET_PUBLIC_KEY") publicKeyS = parts[1];
else if (parts[0] == "AWS_BUCKET_SECRET_KEY") secretKeyS = parts[1];
}

return (publicKeyS, secretKeyS);
}

public async Task<IList<string>> GetThemes()
{
var themes = new List<string>();
Expand Down Expand Up @@ -132,8 +158,9 @@ public async Task<GetObjectResponse> DownloadFile(string keyName)
public async Task<bool> UploadFormFile(IFormFile file, string path = "")
{
s3Client = new AmazonS3Client(publicKey, secretKey, bucketRegion);

var res = await UploadFileAsync(file.OpenReadStream(), path);

if (res == "Ok")
return true;
else
Expand Down
5 changes: 4 additions & 1 deletion src/Blogifier.Shared/Domain/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Blogifier.Shared.Domain
{
public class Configuration
{

[Key]
public string Name { get; set;}

public bool Active { get; set; }
}
}
7 changes: 0 additions & 7 deletions src/Blogifier/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,5 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AWSCredentials": {
"S3Bucket": {
"name": "qvanuncios-bucket",
"accessKey": "AKIAYCXVMZ23PAEXBPWR",
"secretKey": "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"
}
}
}
9 changes: 1 addition & 8 deletions src/Blogifier/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,5 @@
"DbProvider": "SQLite",
"ConnString": "Data Source=Blog.db",
"Salt": "SECRET-CHANGE-ME!"
},
"AWSCredentials": {
"S3Bucket": {
"name": "qvanuncios-bucket",
"accessKey": "AKIAYCXVMZ23PAEXBPWR",
"secretKey": "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"
}
}
}
}

0 comments on commit 4630258

Please sign in to comment.