Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception when writing connection.apiKey values #369

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/SeqCli/Config/EnvironmentOverrides.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// Copyright © Datalust Pty Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -30,7 +43,7 @@ internal static void Apply(string prefix, SeqCliConfig config, Dictionary<string
}
}

internal static string ToEnvironmentVariableName(string prefix, string key)
static string ToEnvironmentVariableName(string prefix, string key)
{
return prefix + key.Replace(".", "_").ToUpperInvariant();
}
Expand Down
29 changes: 23 additions & 6 deletions src/SeqCli/Config/KeyValueSettings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright © Datalust Pty Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -24,7 +38,7 @@ public static void Set(SeqCliConfig config, string key, string? value)
for (var i = 0; i < steps.Length - 1; ++i)
{
var nextStep = receiver.GetType().GetTypeInfo().DeclaredProperties
.Where(p => p.CanRead && p.GetMethod!.IsPublic && !p.GetMethod.IsStatic && p.GetCustomAttribute<ObsoleteAttribute>() == null)
.Where(p => p.CanRead && p.GetMethod!.IsPublic && !p.GetMethod.IsStatic && p.GetCustomAttribute<JsonIgnoreAttribute>() == null)
.SingleOrDefault(p => Camelize(GetUserFacingName(p)) == steps[i]);

if (nextStep == null)
Expand All @@ -38,10 +52,12 @@ public static void Set(SeqCliConfig config, string key, string? value)
throw new InvalidOperationException("Intermediate configuration object is null.");
}

// FUTURE: the use of `p.Name` and lack of `JsonIgnoreAttribute` checks here mean that sensitive values can
// intercept writes through hidden properties, triggering encoding where supported. A type-based solution
// would be more robust.
var targetProperty = receiver.GetType().GetTypeInfo().DeclaredProperties
.Where(p => p is { CanRead: true, CanWrite: true } && p.GetMethod!.IsPublic && p.SetMethod!.IsPublic &&
!p.GetMethod.IsStatic && p.GetCustomAttribute<ObsoleteAttribute>() == null)
.SingleOrDefault(p => Camelize(GetUserFacingName(p)) == steps[^1]);
.Where(p => p is { CanRead: true, CanWrite: true } && p.GetMethod!.IsPublic && p.SetMethod!.IsPublic && !p.GetMethod.IsStatic)
.SingleOrDefault(p => Camelize(p.Name) == steps[^1]);

if (targetProperty == null)
throw new ArgumentException("The key could not be found; run `seqcli config list` to view all keys.");
Expand All @@ -56,7 +72,7 @@ public static void Set(SeqCliConfig config, string key, string? value)
return value?.Split(',').Select(e => e.Trim()).ToArray() ?? [];

if (propertyType == typeof(int[]))
return value?.Split(',').Select(e => int.Parse(e.Trim(), CultureInfo.InvariantCulture)).ToArray() ?? Array.Empty<int>();
return value?.Split(',').Select(e => int.Parse(e.Trim(), CultureInfo.InvariantCulture)).ToArray() ?? [];

if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Expand Down Expand Up @@ -98,7 +114,7 @@ public static bool TryGetValue(object config, string key, out string? value, [No
{
foreach (var nextStep in receiver.GetType().GetTypeInfo().DeclaredProperties
.Where(p => p.CanRead && p.GetMethod!.IsPublic &&
!p.GetMethod.IsStatic && p.GetCustomAttribute<ObsoleteAttribute>() == null)
!p.GetMethod.IsStatic && p.GetCustomAttribute<ObsoleteAttribute>() == null && p.GetCustomAttribute<JsonIgnoreAttribute>() == null)
.OrderBy(GetUserFacingName))
{
var camel = Camelize(GetUserFacingName(nextStep));
Expand Down Expand Up @@ -131,6 +147,7 @@ public static bool TryGetValue(object config, string key, out string? value, [No
else if (nextStep.CanRead && nextStep.GetMethod!.IsPublic &&
nextStep.CanWrite && nextStep.SetMethod!.IsPublic &&
!nextStep.SetMethod.IsStatic &&
nextStep.GetCustomAttribute<ObsoleteAttribute>() == null &&
nextStep.GetCustomAttribute<JsonIgnoreAttribute>() == null)
{
var value = nextStep.GetValue(receiver);
Expand Down
14 changes: 14 additions & 0 deletions src/SeqCli/Config/RuntimeConfigurationLoader.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright © Datalust Pty Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.IO;

Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Config/SeqCliConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void WriteToFile(SeqCliConfig data, string filename)
{
if (!data._exportable)
throw new InvalidOperationException("The provided configuration is not exportable.");

var content = JsonConvert.SerializeObject(data, Formatting.Indented, SerializerSettings);
File.WriteAllText(filename, content);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"profiles": {
"SeqCli": {
"commandName": "Project",
"commandLineArgs": "signal update --json-stdin"
"commandLineArgs": "config -k connection.apiKey -v test"
}
}
}