Allow token refresh for GCP (#402)

This commit is contained in:
Andrew Stakhov
2020-04-28 18:34:25 -04:00
committed by GitHub
parent cfc4306528
commit ae9dd04a2e
10 changed files with 186 additions and 18 deletions

View File

@@ -0,0 +1,28 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using k8s.Authentication;
using Xunit;
namespace k8s.Tests
{
public class GcpTokenProviderTests
{
[OperatingSystemDependentFact(Exclude = OperatingSystem.OSX)]
public async Task GetToken()
{
var isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
var cmd = Path.Combine(Directory.GetCurrentDirectory(), "assets", isWindows ? "mock-gcloud.cmd" : "mock-gcloud.sh");
if (!isWindows)
{
System.Diagnostics.Process.Start("chmod", $"+x {cmd}").WaitForExit();
}
var sut = new GcpTokenProvider(cmd);
var result = await sut.GetAuthenticationHeaderAsync(CancellationToken.None);
result.Scheme.Should().Be("Bearer");
result.Parameter.Should().Be("ACCESS-TOKEN");
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
namespace k8s.Tests
{
[Flags]
public enum OperatingSystem
{
Windows = 1,
Linux = 2,
OSX = 4
}
}

View File

@@ -0,0 +1,37 @@
using System.Runtime.InteropServices;
using Xunit;
namespace k8s.Tests
{
public class OperatingSystemDependentFactAttribute : FactAttribute
{
public OperatingSystem Include { get; set; } = OperatingSystem.Linux | OperatingSystem.Windows | OperatingSystem.OSX;
public OperatingSystem Exclude { get; set; }
public override string Skip
{
get => IsOS(Include) && !IsOS(Exclude) ? null : "Not compatible with current OS";
set { }
}
private bool IsOS(OperatingSystem operatingSystem)
{
if (operatingSystem.HasFlag(OperatingSystem.Linux) && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return true;
}
if (operatingSystem.HasFlag(OperatingSystem.Windows) && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return true;
}
if (operatingSystem.HasFlag(OperatingSystem.OSX) && RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,23 @@
{
"configuration": {
"active_configuration": "default",
"properties": {
"compute": {
"region": "us-east1",
"zone": "us-east1-b"
},
"core": {
"account": "some@account.io",
"disable_usage_reporting": "True",
"project": "fe-astakhov"
}
}
},
"credential": {
"access_token": "ACCESS-TOKEN",
"token_expiry": "2020-03-20T07:09:20Z"
},
"sentinels": {
"config_sentinel": "C:\\Users\\Andrew\\AppData\\Roaming\\gcloud\\config_sentinel"
}
}

View File

@@ -0,0 +1,2 @@
@echo off
type %~dp0\gcloud-config-helper.json

View File

@@ -0,0 +1,5 @@
#!/bin/bash
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
OUTPUT_JSON=$SCRIPTPATH/gcloud-config-helper.json
cat $OUTPUT_JSON