From 610aa05a38c36d0e10b745d45d85f6f4afe79a2c Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 23 Mar 2023 15:57:51 +0100 Subject: [PATCH 1/5] Wip test commit --- .../App/OpenVpnCertificatesServer/Program.cs | 28 +++++++++++++------ .../App/OpenVpnCertificatesServer/newToken.sh | 19 +++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/csharp/App/OpenVpnCertificatesServer/Program.cs b/csharp/App/OpenVpnCertificatesServer/Program.cs index c9bff3a03..54b426516 100644 --- a/csharp/App/OpenVpnCertificatesServer/Program.cs +++ b/csharp/App/OpenVpnCertificatesServer/Program.cs @@ -1,5 +1,8 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Net; using System.Text; +using System.Text.Json; using Flurl; using ICSharpCode.SharpZipLib.Tar; using InnovEnergy.App.OpenVpnCertificatesServer.PKI; @@ -18,6 +21,7 @@ namespace InnovEnergy.App.OpenVpnCertificatesServer; // quotes!! // wget 'http://localhost:4000/get_cert?name=MYNAME&pw=MwBRbQb3QaX7l9XIaakq' +[SuppressMessage("Trimming", "IL2026:Members annotated with \'RequiresUnreferencedCodeAttribute\' require dynamic access otherwise can break functionality when trimming application code")] public static class Program { // TODO: use fody weaver to obfuscate strings? @@ -25,13 +29,13 @@ public static class Program private const String VpnSubnet = "10.2."; private const String VpnGateway = "10.2.0.1"; - private const String VpnDir = "/etc/openvpn/server/Salino"; - private const String CcdDir = VpnDir + "/ccd"; - private const String CertsDir = VpnDir + "/certs"; - private const String Endpoint = "http://localhost:4000/"; - private const String VrmUser = "victron@innov.energy"; - private const String VrmPwd = "NnoVctr201002"; - private const String ManualPw = "MwBRbQb3QaX7l9XIaakq"; + private const String VpnDir = "/etc/openvpn/server/Salino"; + private const String CcdDir = VpnDir + "/ccd"; + private const String CertsDir = VpnDir + "/certs"; + private const String Endpoint = "http://localhost:4000/"; + + private static readonly AccToken AccToken = JsonSerializer.Deserialize(File.OpenRead("./token.json"))!; //"d4179e69413ad8c507e0965a55bb90fe712184af9c81c196b9d19db5bb083d5f"; + private const String ManualPw = "MwBRbQb3QaX7l9XIaakq"; private const String QueryPath = "/get_cert"; private const String NameQueryParam = "name"; @@ -134,7 +138,7 @@ public static class Program //var installationName = await LookupInstallationNameByUniqueId(ccdName); - using var vrm = await VrmAccount.Login(VrmUser, VrmPwd); // TODO: use token + using var vrm = VrmAccount.Token(AccToken.Id, AccToken.Token); var installations = await vrm.GetInstallations(); var installationName = installations @@ -151,7 +155,7 @@ public static class Program { Console.WriteLine($"looking up {ccdName} on VRM"); - using var vrm = await VrmAccount.Login(VrmUser, VrmPwd); // TODO: use token + using var vrm = VrmAccount.Token(AccToken.Id, AccToken.Token); var installations = await vrm.GetInstallations(); foreach (var installation in installations) @@ -265,4 +269,10 @@ public static class Program return $"{VpnSubnet}{hi}.{lo}"; } +} + +internal interface AccToken +{ + public String Token { get; init; } + public UInt64 Id { get; init; } } \ No newline at end of file diff --git a/csharp/App/OpenVpnCertificatesServer/newToken.sh b/csharp/App/OpenVpnCertificatesServer/newToken.sh index e69de29bb..beda996eb 100644 --- a/csharp/App/OpenVpnCertificatesServer/newToken.sh +++ b/csharp/App/OpenVpnCertificatesServer/newToken.sh @@ -0,0 +1,19 @@ +#!/bin/bash +## USAGE: ./newToken.sh -u USERNAME -p PASSWORD + + +while getopts u:p: flag +do + case "${flag}" in + u) username=${OPTARG};; + p) password=${OPTARG};; + esac +done + +curl --request POST \ + --url https://vrmapi.victronenergy.com/v2/auth/login \ + --header 'Content-Type: application/json' \ + --data '{ + "username": '$username', + "password": '$password', +}' > token.json \ No newline at end of file From 888a9f716b8a01e09369a7fbefa5a76e4fb8cf8e Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 23 Mar 2023 16:57:50 +0100 Subject: [PATCH 2/5] added token generator and getting token from token.json --- .../App/OpenVpnCertificatesServer/Program.cs | 18 ++++++++------ .../App/OpenVpnCertificatesServer/newToken.sh | 24 ++++++++++++++----- .../App/OpenVpnCertificatesServer/token.json | 1 + 3 files changed, 30 insertions(+), 13 deletions(-) mode change 100644 => 100755 csharp/App/OpenVpnCertificatesServer/newToken.sh create mode 100644 csharp/App/OpenVpnCertificatesServer/token.json diff --git a/csharp/App/OpenVpnCertificatesServer/Program.cs b/csharp/App/OpenVpnCertificatesServer/Program.cs index 54b426516..436a5ff6d 100644 --- a/csharp/App/OpenVpnCertificatesServer/Program.cs +++ b/csharp/App/OpenVpnCertificatesServer/Program.cs @@ -1,6 +1,5 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Net; using System.Text; using System.Text.Json; using Flurl; @@ -125,7 +124,7 @@ public static class Program Console.WriteLine($"ccd name {ccdName} is already in use\n"); return InvalidRequest; } - + return ps.Contains(NameQueryParam) ? ParseManualRequest(ps, ccdName!) : ps.Contains(UniqueIdQueryParam) ? await LookupInstallationNameByUniqueId(ccdName!) : ps.Contains(MachineSerialQueryParam) ? await LookupInstallationNameByMachineSerial(ccdName!) @@ -138,7 +137,7 @@ public static class Program //var installationName = await LookupInstallationNameByUniqueId(ccdName); - using var vrm = VrmAccount.Token(AccToken.Id, AccToken.Token); + using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token); var installations = await vrm.GetInstallations(); var installationName = installations @@ -154,8 +153,8 @@ public static class Program private static async Task<(String ccdName, String humanReadableName)?> LookupInstallationNameByMachineSerial(String ccdName) { Console.WriteLine($"looking up {ccdName} on VRM"); - - using var vrm = VrmAccount.Token(AccToken.Id, AccToken.Token); + + using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token); var installations = await vrm.GetInstallations(); foreach (var installation in installations) @@ -273,6 +272,11 @@ public static class Program internal interface AccToken { - public String Token { get; init; } - public UInt64 Id { get; init; } + public String token { get; init; } + public String bearer { get; init; } + public UInt64 idUser { get; init; } + public String verification_mode { get; init; } + public String idAccessToken { get; init; } + public Boolean verification_sent { get; init; } + public Boolean success { get; init; } } \ No newline at end of file diff --git a/csharp/App/OpenVpnCertificatesServer/newToken.sh b/csharp/App/OpenVpnCertificatesServer/newToken.sh old mode 100644 new mode 100755 index beda996eb..31f5fa90a --- a/csharp/App/OpenVpnCertificatesServer/newToken.sh +++ b/csharp/App/OpenVpnCertificatesServer/newToken.sh @@ -2,18 +2,30 @@ ## USAGE: ./newToken.sh -u USERNAME -p PASSWORD -while getopts u:p: flag +while getopts u:p:n: flag do case "${flag}" in u) username=${OPTARG};; p) password=${OPTARG};; + n) name=${OPTARG};; esac done -curl --request POST \ +response=$(curl --request POST \ --url https://vrmapi.victronenergy.com/v2/auth/login \ --header 'Content-Type: application/json' \ - --data '{ - "username": '$username', - "password": '$password', -}' > token.json \ No newline at end of file + --data '{"username":"'$username'","password":"'$password'","sms_token":null,"remember_me":false,"language":""}') + +echo -n '{"bearer' > token.json +token=$(echo $response | jq ".token") +#echo ${token:1:-1} +uid=$(echo $response | jq ".idUser") +echo -n ${response:7:-1} >> token.json + +response2=$(curl --request POST \ + --url https://vrmapi.victronenergy.com/v2/users/$uid/accesstokens/create \ + --header 'Content-Type: application/json' \ + --header 'x-authorization: Bearer '${token:1:-1} \ + --data '{"name":"'$name'"}') +echo -n , >> token.json +echo -n ${response2:1} >> token.json \ No newline at end of file diff --git a/csharp/App/OpenVpnCertificatesServer/token.json b/csharp/App/OpenVpnCertificatesServer/token.json new file mode 100644 index 000000000..456c09453 --- /dev/null +++ b/csharp/App/OpenVpnCertificatesServer/token.json @@ -0,0 +1 @@ +{"bearer":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjJmYzY5NTNiMjhjNTg3NWQyMzQwNzM3ZjlhNGIzM2RjIn0.eyJ1aWQiOiI1NTQ1MCIsInRva2VuX3R5cGUiOiJkZWZhdWx0IiwiaXNzIjoidnJtYXBpLnZpY3Ryb25lbmVyZ3kuY29tIiwiYXVkIjoiaHR0cHM6Ly92cm1hcGkudmljdHJvbmVuZXJneS5jb20vIiwiaWF0IjoxNjc5NTg2ODU0LCJleHAiOjE2Nzk2NzMyNTQsImp0aSI6IjJmYzY5NTNiMjhjNTg3NWQyMzQwNzM3ZjlhNGIzM2RjIn0.AsLJU7qDPBHO-_FjVo9a8RbyoxhYrDrwZX7V3z4Xq8EoUulv2VyTqy9OXLpez4JI2FVAfSO5a7Amj4XvK1AWtDr9MxP07IBfPyMu5LLGTzjPMAJ6fvZbvZ-eRsP1-aARCFekMGoeSvEEBDrZB9_0kps4h9idQwwGwAby2Tya0vNGu6QEw9WmHcbL8qjvJzxavg8bK6Lttv2-3l_11iZoqiYLdXbwBi32GYK_sdwp-fbGvPE1d6g6eVll94JfSqNLZl9baijtOksx_Qouu7YB8knCgFNrx535d4iJtCkMv9xWztWXbevpSQiy9S8pCgLSpmHNztlVDjacEYyduwUzyw","idUser":55450,"verification_mode":"password","verification_sent":false,"success":true,"token":"108a1407da84afea182b8102a202a2c6b73e9bd6ad919f521033b09837e6e564","idAccessToken":"385119"} \ No newline at end of file From f62375ae54f8add8bc6316bfe29185907e0ea29b Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 23 Mar 2023 16:58:26 +0100 Subject: [PATCH 3/5] added token generator and getting token from token.json --- csharp/App/OpenVpnCertificatesServer/newToken.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/csharp/App/OpenVpnCertificatesServer/newToken.sh b/csharp/App/OpenVpnCertificatesServer/newToken.sh index 31f5fa90a..22f34698f 100755 --- a/csharp/App/OpenVpnCertificatesServer/newToken.sh +++ b/csharp/App/OpenVpnCertificatesServer/newToken.sh @@ -1,5 +1,5 @@ #!/bin/bash -## USAGE: ./newToken.sh -u USERNAME -p PASSWORD +## USAGE: ./newToken.sh -u Username -p Password -n UniqueTokenName while getopts u:p:n: flag @@ -18,7 +18,7 @@ response=$(curl --request POST \ echo -n '{"bearer' > token.json token=$(echo $response | jq ".token") -#echo ${token:1:-1} + uid=$(echo $response | jq ".idUser") echo -n ${response:7:-1} >> token.json From b8da0c379d8a24223fa764cbc58f8df376d20570 Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 23 Mar 2023 17:17:10 +0100 Subject: [PATCH 4/5] refactoring and testing OpenVpnCertificatesServer with token --- .../App/OpenVpnCertificatesServer/AccToken.cs | 12 ++++++++ .../OpenVpnCertificatesServer.csproj | 6 ++++ .../App/OpenVpnCertificatesServer/Program.cs | 29 ++++++++----------- 3 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 csharp/App/OpenVpnCertificatesServer/AccToken.cs diff --git a/csharp/App/OpenVpnCertificatesServer/AccToken.cs b/csharp/App/OpenVpnCertificatesServer/AccToken.cs new file mode 100644 index 000000000..1051797c6 --- /dev/null +++ b/csharp/App/OpenVpnCertificatesServer/AccToken.cs @@ -0,0 +1,12 @@ +namespace InnovEnergy.App.OpenVpnCertificatesServer; + +internal class AccToken +{ + public String token { get; init; } + public String bearer { get; init; } + public UInt64 idUser { get; init; } + public String verification_mode { get; init; } + public String idAccessToken { get; init; } + public Boolean verification_sent { get; init; } + public Boolean success { get; init; } +} \ No newline at end of file diff --git a/csharp/App/OpenVpnCertificatesServer/OpenVpnCertificatesServer.csproj b/csharp/App/OpenVpnCertificatesServer/OpenVpnCertificatesServer.csproj index e44b0a11e..96abef819 100644 --- a/csharp/App/OpenVpnCertificatesServer/OpenVpnCertificatesServer.csproj +++ b/csharp/App/OpenVpnCertificatesServer/OpenVpnCertificatesServer.csproj @@ -12,5 +12,11 @@ + + + + PreserveNewest + + diff --git a/csharp/App/OpenVpnCertificatesServer/Program.cs b/csharp/App/OpenVpnCertificatesServer/Program.cs index 436a5ff6d..024cc61b0 100644 --- a/csharp/App/OpenVpnCertificatesServer/Program.cs +++ b/csharp/App/OpenVpnCertificatesServer/Program.cs @@ -32,8 +32,14 @@ public static class Program private const String CcdDir = VpnDir + "/ccd"; private const String CertsDir = VpnDir + "/certs"; private const String Endpoint = "http://localhost:4000/"; - - private static readonly AccToken AccToken = JsonSerializer.Deserialize(File.OpenRead("./token.json"))!; //"d4179e69413ad8c507e0965a55bb90fe712184af9c81c196b9d19db5bb083d5f"; + + private static AccToken ReadAccessToken() + { + var content = File.ReadAllText("./token.json"); + return JsonSerializer.Deserialize(content)!; + } + + private const String ManualPw = "MwBRbQb3QaX7l9XIaakq"; private const String QueryPath = "/get_cert"; @@ -136,8 +142,8 @@ public static class Program Console.WriteLine($"looking up unique id {uniqueId} on VRM"); //var installationName = await LookupInstallationNameByUniqueId(ccdName); - - using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token); + var token = ReadAccessToken(); + using var vrm = VrmAccount.Token(token.idUser, token.token); var installations = await vrm.GetInstallations(); var installationName = installations @@ -153,8 +159,8 @@ public static class Program private static async Task<(String ccdName, String humanReadableName)?> LookupInstallationNameByMachineSerial(String ccdName) { Console.WriteLine($"looking up {ccdName} on VRM"); - - using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token); + var token = ReadAccessToken(); + using var vrm = VrmAccount.Token(token.idUser, token.token); var installations = await vrm.GetInstallations(); foreach (var installation in installations) @@ -268,15 +274,4 @@ public static class Program return $"{VpnSubnet}{hi}.{lo}"; } -} - -internal interface AccToken -{ - public String token { get; init; } - public String bearer { get; init; } - public UInt64 idUser { get; init; } - public String verification_mode { get; init; } - public String idAccessToken { get; init; } - public Boolean verification_sent { get; init; } - public Boolean success { get; init; } } \ No newline at end of file From eb033294414ca7c38fb2f2a47fca16b01bf17531 Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 23 Mar 2023 17:36:24 +0100 Subject: [PATCH 5/5] supressing warnings --- csharp/App/OpenVpnCertificatesServer/AccToken.cs | 5 +++++ csharp/App/OpenVpnCertificatesServer/Program.cs | 3 +++ 2 files changed, 8 insertions(+) diff --git a/csharp/App/OpenVpnCertificatesServer/AccToken.cs b/csharp/App/OpenVpnCertificatesServer/AccToken.cs index 1051797c6..a12ee720c 100644 --- a/csharp/App/OpenVpnCertificatesServer/AccToken.cs +++ b/csharp/App/OpenVpnCertificatesServer/AccToken.cs @@ -1,5 +1,10 @@ +using System.Diagnostics.CodeAnalysis; +#pragma warning disable CS8618 + namespace InnovEnergy.App.OpenVpnCertificatesServer; +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] +[SuppressMessage("ReSharper", "InconsistentNaming")] internal class AccToken { public String token { get; init; } diff --git a/csharp/App/OpenVpnCertificatesServer/Program.cs b/csharp/App/OpenVpnCertificatesServer/Program.cs index 024cc61b0..a3e035fbe 100644 --- a/csharp/App/OpenVpnCertificatesServer/Program.cs +++ b/csharp/App/OpenVpnCertificatesServer/Program.cs @@ -12,7 +12,10 @@ using static InnovEnergy.App.OpenVpnCertificatesServer.PKI.CertificateAuthority; namespace InnovEnergy.App.OpenVpnCertificatesServer; +// export SolutionDir=$(pwd) // dotnet publish OpenVpnCertificatesServer.csproj -c Release -r linux-x64 -p:PublishSingleFile=true --self-contained true && scp bin/Release/net6.0/linux-x64/publish/OpenVpnCertificatesServer ig@salidomo.innovenergy.ch:~/get_cert/get_cert +// scp bin/Release/net6.0/linux-x64/publish/token.json ig@salidomo.innovenergy.ch:~/get_cert/token.json + // http://localhost:4000/get_cert?machine_serial=HQ2032UAWYM // http://localhost:4000/get_cert?unique_id=985dadd0cf29