diff options
| author | crupest <crupest@outlook.com> | 2022-12-02 18:43:17 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:52 +0800 | 
| commit | 87fc365b7debf990aab668783401c746f8e7cd3e (patch) | |
| tree | 4c20ee1fb813898d31220eeb18ee9973c4ad9e77 /docker/crupest-api/CrupestApi/CrupestApi.Secrets | |
| parent | 84167119c0282a5e19b8e7bfb578487c3cddf2d8 (diff) | |
| download | crupest-87fc365b7debf990aab668783401c746f8e7cd3e.tar.gz crupest-87fc365b7debf990aab668783401c746f8e7cd3e.tar.bz2 crupest-87fc365b7debf990aab668783401c746f8e7cd3e.zip  | |
Develop secret api.
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Secrets')
| -rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Secrets/CrupestApi.Secrets.csproj | 5 | ||||
| -rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs | 109 | 
2 files changed, 114 insertions, 0 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Secrets/CrupestApi.Secrets.csproj b/docker/crupest-api/CrupestApi/CrupestApi.Secrets/CrupestApi.Secrets.csproj index 86460e3..70c83f3 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Secrets/CrupestApi.Secrets.csproj +++ b/docker/crupest-api/CrupestApi/CrupestApi.Secrets/CrupestApi.Secrets.csproj @@ -4,6 +4,11 @@      <ProjectReference Include="..\CrupestApi.Commons\CrupestApi.Commons.csproj" />
    </ItemGroup>
 +  <ItemGroup>
 +    <PackageReference Include="Dapper" Version="2.0.123" />
 +    <PackageReference Include="Microsoft.Data.Sqlite" Version="7.0.0" />
 +  </ItemGroup>
 +
    <PropertyGroup>
      <TargetFramework>net7.0</TargetFramework>
      <TargetType>library</TargetType>
 diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs new file mode 100644 index 0000000..3913a0b --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs @@ -0,0 +1,109 @@ +using CrupestApi.Commons; +using Dapper; +using Microsoft.Data.Sqlite; +using Microsoft.Extensions.Options; + +namespace CrupestApi.Secrets; + +public class SecretsService : ISecretsService +{ +    private readonly IOptionsSnapshot<CrupestApiConfig> _crupestApiConfig; +    private readonly ILogger<SecretsService> _logger; + +    public SecretsService(IOptionsSnapshot<CrupestApiConfig> crupestApiConfig, ILogger<SecretsService> logger) +    { +        _crupestApiConfig = crupestApiConfig; +        _logger = logger; +    } + +    private string GetDatabasePath() +    { +        return Path.Combine(_crupestApiConfig.Value.DataDir, "secrets.db"); +    } + +    private async Task<SqliteConnection> EnsureDatabase() +    { +        var dataSource = GetDatabasePath(); +        var connectionStringBuilder = new SqliteConnectionStringBuilder() +        { +            DataSource = dataSource +        }; + +        if (!File.Exists(dataSource)) +        { +            _logger.LogInformation("Data source {0} does not exist. Create one.", dataSource); +            connectionStringBuilder.Mode = SqliteOpenMode.ReadWriteCreate; +            var connectionString = connectionStringBuilder.ToString(); +            var connection = new SqliteConnection(connectionString); +            var transaction = await connection.BeginTransactionAsync(); + +            connection.Execute(@" +CREATE TABLE secrets ( +    Id INTEGER PRIMARY KEY AUTOINCREMENT, +    Key TEXT NOT NULL, +    Secret TEXT NOT NULL, +    Description TEXT NOT NULL, +    ExpireTime TEXT, +    Revoked INTEGER NOT NULL, +    CreateTime TEXT NOT NULL +); + +CREATE INDEX secrets_key ON secrets (key); + +INSERT INTO secrets (Key, Secret, Description, ExpireTime, Revoked, CreateTime) VALUES (@SecretManagementKey, 'crupest', 'This is the default secret management key.', NULL, 0, @CreateTime); +            ", +            new +            { +                SecretManagementKey = SecretsConstants.SecretManagementKey, +                CreateTime = DateTime.Now.ToString("O"), +            }); + +            await transaction.CommitAsync(); + +            _logger.LogInformation("{0} created with 'crupest' as the default secret management value. Please immediate revoke it and create a new one.", dataSource); +            return connection; +        } +        else +        { +            _logger.LogInformation("Data source {0} already exists. Will use it."); +            connectionStringBuilder.Mode = SqliteOpenMode.ReadWrite; +            var connectionString = connectionStringBuilder.ToString(); +            return new SqliteConnection(connectionString); +        } +    } + +    public Task<SecretInfo> CreateSecretAsync(string key, string description, DateTime? expireTime = null) +    { +        throw new NotImplementedException(); +    } + +    public Task<List<SecretInfo>> GetSecretListAsync(bool includeExpired = false, bool includeRevoked = false) +    { +        throw new NotImplementedException(); +    } + +    public Task<List<SecretInfo>> GetSecretListByKeyAsync(string key, bool includeExpired = false, bool includeRevoked = false) +    { +        throw new NotImplementedException(); +    } + +    public Task<SecretInfo> ModifySecretAsync(string secret, SecretModifyRequest modifyRequest) +    { +        throw new NotImplementedException(); +    } + +    public Task RevokeSecretAsync(string secret) +    { +        throw new NotImplementedException(); +    } + +    public Task<bool> VerifySecretAsync(string key, string secret) +    { +        throw new NotImplementedException(); +    } + +    public Task VerifySecretForHttpRequestAsync(HttpRequest request, string key, string queryKey = "secret") +    { +        throw new NotImplementedException(); +    } +}  | 
