Пример #1
0
        public async ValueTask <KeyVaultSecret> GetSecret(string name)
        {
            using (var conn = new NpgsqlConnection(_connectionString)) {
                await conn.OpenAsync().NoSync();

                KeyVaultSecret result;
                Dictionary <long, KeyVaultSecretAccess> access = new Dictionary <long, KeyVaultSecretAccess>();
                Dictionary <string, KeyVaultSecretData> data   = new Dictionary <string, KeyVaultSecretData>();
                using (var cmd = new NpgsqlCommand("SELECT \"A\".\"Id\", \"A\".\"Name\", \"A\".\"Description\", \"A\".\"CreateDate\", \"A\".\"CreatorUserId\", \"A\".\"LastUpdateDate\", \"A\".\"LastUpdateUserId\" FROM \"Secret\" \"A\" WHERE \"A\".\"Name\" = @name;", conn)) {
                    cmd.Parameters.AddWithValue("@name", name);

                    var reader = await cmd.ExecuteReaderAsync().NoSync();

                    await using (reader.NoSync()) {
                        if (!await reader.ReadAsync().NoSync())
                        {
                            return(null);
                        }

                        result = new KeyVaultSecret(reader.GetInt64(0), reader.GetString(1), await GetNullableString(reader, 2).NoSync(), EnsureDateTime(reader.GetDateTime(3)), await GetNullableInt64(reader, 4).NoSync() ?? -1, EnsureDateTime(await GetNullableDateTime(reader, 5).NoSync()), await GetNullableInt64(reader, 6).NoSync(), data, access);
                    }
                }

                using (var cmd = new NpgsqlCommand("SELECT \"A\".\"UserId\", \"A\".\"Read\", \"A\".\"Write\", \"A\".\"Assign\" FROM \"SecretAccess\" \"A\" WHERE \"A\".\"SecretId\" = @secretId ORDER BY \"A\".\"UserId\" ASC;", conn)) {
                    cmd.Parameters.AddWithValue("@secretId", result.Id);

                    var reader = await cmd.ExecuteReaderAsync().NoSync();

                    await using (reader.NoSync()) {
                        while (await reader.ReadAsync().NoSync())
                        {
                            var a = new KeyVaultSecretAccess(result, reader.GetInt64(0), reader.GetBoolean(1), reader.GetBoolean(2), reader.GetBoolean(3));
                            access.Add(a.UserId, a);
                        }
                    }
                }

                using (var cmd = new NpgsqlCommand("SELECT \"A\".\"Name\", \"A\".\"Description\", \"A\".\"Type\", \"A\".\"CreateDate\", \"A\".\"CreatorUserId\", \"A\".\"LastUpdateDate\", \"A\".\"LastUpdateUserId\" FROM \"SecretData\" \"A\" WHERE \"A\".\"SecretId\" = @secretId ORDER BY \"A\".\"Name\" ASC;", conn)) {
                    cmd.Parameters.AddWithValue("@secretId", result.Id);

                    var reader = await cmd.ExecuteReaderAsync().NoSync();

                    await using (reader.NoSync()) {
                        while (await reader.ReadAsync().NoSync())
                        {
                            CommonUtility.TryParseEnum(reader.GetString(2), out KeyVaultSecretType secretType);
                            var a = new KeyVaultSecretData(result, await GetNullableString(reader, 0).NoSync(), await GetNullableString(reader, 1).NoSync(), secretType, EnsureDateTime(reader.GetDateTime(3)), await GetNullableInt64(reader, 4).NoSync() ?? -1, EnsureDateTime(await GetNullableDateTime(reader, 5).NoSync()), await GetNullableInt64(reader, 6).NoSync());
                            data.Add(a.Name ?? string.Empty, a);
                        }
                    }
                }

                return(result);
            }
        }
Пример #2
0
        public async ValueTask <KeyVaultSecret> GetSecret(string name)
        {
            using (var conn = new SqliteConnection(_connectionString)) {
                await conn.OpenAsync().NoSync();

                KeyVaultSecret result;
                Dictionary <long, KeyVaultSecretAccess> access = new Dictionary <long, KeyVaultSecretAccess>();
                Dictionary <string, KeyVaultSecretData> data   = new Dictionary <string, KeyVaultSecretData>();
                using (var cmd = new SqliteCommand("SELECT [A].[Id], [A].[Name], [A].[Description], [A].[CreateDate], [A].[CreatorUserId], [A].[LastUpdateDate], [A].[LastUpdateUserId] FROM [Secret] [A] WHERE [A].[Name] = @name;", conn)) {
                    cmd.Parameters.AddWithValue("@name", name);

                    var reader = await cmd.ExecuteReaderAsync().NoSync();

                    await using (reader.NoSync()) {
                        if (!await reader.ReadAsync().NoSync())
                        {
                            return(null);
                        }

                        result = new KeyVaultSecret(reader.GetInt64(0), reader.GetString(1), await GetNullableString(reader, 2).NoSync(), CommonUtility.GetDateTimeFromTimestamp(reader.GetInt64(3)), await GetNullableInt64(reader, 4).NoSync() ?? -1, FromNullableTimestamp(await GetNullableInt64(reader, 5).NoSync()), await GetNullableInt64(reader, 6).NoSync(), data, access);
                    }
                }

                using (var cmd = new SqliteCommand("SELECT [A].[UserId], [A].[Read], [A].[Write], [A].[Assign] FROM [SecretAccess] [A] WHERE [A].[SecretId] = @secretId ORDER BY [A].[UserId] ASC;", conn)) {
                    cmd.Parameters.AddWithValue("@secretId", result.Id);

                    var reader = await cmd.ExecuteReaderAsync().NoSync();

                    await using (reader.NoSync()) {
                        while (await reader.ReadAsync().NoSync())
                        {
                            var a = new KeyVaultSecretAccess(result, reader.GetInt64(0), reader.GetBoolean(1), reader.GetBoolean(2), reader.GetBoolean(3));
                            access.Add(a.UserId, a);
                        }
                    }
                }

                using (var cmd = new SqliteCommand("SELECT [A].[Name], [A].[Description], [A].[Type], [A].[CreateDate], [A].[CreatorUserId], [A].[LastUpdateDate], [A].[LastUpdateUserId] FROM [SecretData] [A] WHERE [A].[SecretId] = @secretId ORDER BY [A].[Name] ASC;", conn)) {
                    cmd.Parameters.AddWithValue("@secretId", result.Id);

                    var reader = await cmd.ExecuteReaderAsync().NoSync();

                    await using (reader.NoSync()) {
                        while (await reader.ReadAsync().NoSync())
                        {
                            CommonUtility.TryParseEnum(reader.GetString(2), out KeyVaultSecretType secretType);
                            var a = new KeyVaultSecretData(result, await GetNullableString(reader, 0).NoSync(), await GetNullableString(reader, 1).NoSync(), secretType, CommonUtility.GetDateTimeFromTimestamp(reader.GetInt64(3)), await GetNullableInt64(reader, 4).NoSync() ?? -1, FromNullableTimestamp(await GetNullableInt64(reader, 5).NoSync()), await GetNullableInt64(reader, 6).NoSync());
                            data.Add(a.Name ?? string.Empty, a);
                        }
                    }
                }

                return(result);
            }
        }