Skip to content

Commit

Permalink
Fix env vars not being properly setup by buildpack
Browse files Browse the repository at this point in the history
  • Loading branch information
macsux committed Jan 27, 2022
1 parent ba189cc commit 014b134
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"Publish",
"PublishSample",
"Release",
"Restore",
"Supply"
]
}
Expand All @@ -99,6 +100,7 @@
"Publish",
"PublishSample",
"Release",
"Restore",
"Supply"
]
}
Expand Down
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ applications:
- dotnet_core_buildpack
env:
KRB5_KDC: ad.almirex.com
KRB_SERVICE_ACCOUNT: [email protected]
KRB_SERVICE_ACCOUNT: [email protected] # MUST BE in format sAMAccountName@KerberosRealm
KRB_PASSWORD: P@ssw0rd

```


** Adjust URL of the Kerberos buildpack to latest version. You can get the full zip URL from [Releases](https://github.com/macsux/kerberos-buildpack/releases) page.

## How it works
Expand Down
21 changes: 17 additions & 4 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public bool IsGitHubRepository
Target Publish => _ => _
.Description("Packages buildpack in Cloud Foundry expected format into /artifacts directory")
.After(Clean)
.DependsOn(PublishSample)
.DependsOn(PublishSample, Restore)
.Executes(() =>
{
var workDirectory = TemporaryDirectory / "pack";
Expand All @@ -111,6 +111,7 @@ public bool IsGitHubRepository
.SetFramework(Framework)
.SetRuntime(Runtime)
.EnableSelfContained()
.EnableNoRestore()
.SetAssemblyVersion(GitVersion.AssemblyVersion)
.SetFileVersion(GitVersion.AssemblyFileVersion)
.SetInformationalVersion(GitVersion.AssemblyInformationalVersion)
Expand Down Expand Up @@ -138,13 +139,25 @@ public bool IsGitHubRepository
Logger.Block(ArtifactsDirectory / PackageZipName);
});

Target Restore => _ => _
.Executes(() =>
{
DotNetRestore(c => c
.SetProjectFile(Solution));

DotNetPublish(c => c
.SetProject(RootDirectory / "sample" / "Samples.sln"));
});

Target PublishSample => _ => _
.DependsOn(Restore)
.Executes(() =>
{
var demoProjectDirectory = RootDirectory / "sample" / "KerberosDemo";
// DotNetPublish(c => c
// .SetProject(demoProjectDirectory / "KerberosDemo.csproj")
// .SetConfiguration("DEBUG"));
DotNetPublish(c => c
.SetProject(demoProjectDirectory / "KerberosDemo.csproj")
.EnableNoRestore()
.SetConfiguration("DEBUG"));
var publishFolder = demoProjectDirectory / "bin" / "Debug" / "net5.0" / "publish";
var manifestFile = publishFolder / "manifest.yml";
var manifest = File.ReadAllText(manifestFile);
Expand Down
68 changes: 64 additions & 4 deletions sample/KerberosDemo/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using File = System.IO.File;
using System.Net.Sockets;
using System.Security.Claims;
using System.Text;
Expand Down Expand Up @@ -132,8 +132,68 @@ public async Task<string> TestKDC(string kdc)
return $"Failed connection test to {kdc} on port 88\n{e}";
}
}



[HttpGet("/diag")]
public async Task<string> Diagnostics()
{
var sb = new StringBuilder();
VerifyEnvVar("KRB5_CONFIG");
VerifyEnvVar("KRB5CCNAME");
VerifyEnvVar("KRB5_KTNAME");

var krb5Conf = Environment.GetEnvironmentVariable("KRB5_CONFIG");
if (!string.IsNullOrEmpty(krb5Conf))
{
sb.AppendLine($"[{krb5Conf} content]");
sb.AppendLine(System.IO.File.ReadAllText(krb5Conf));
}

void VerifyEnvVar(string var)
{
var varValue = Environment.GetEnvironmentVariable(var);
sb.AppendLine($"{var}={varValue}");
if (!string.IsNullOrEmpty(varValue))
{
var fileExists = System.IO.File.Exists(varValue);
sb.AppendLine($"{varValue} = {(fileExists ? "exists" : "missing")}");
}
}

return sb.ToString();
}

[HttpGet("/run")]
private string Run(string command)
{
var commandSegments = command.Split(" ");
var processName = commandSegments[0];
var args = command[1..];
// Start the child process.
try
{


Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = processName;
p.StartInfo.Arguments = string.Join(" ", args);
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
var output = p.StandardOutput.ReadToEnd();
var error = p.StandardError.ReadToEnd();
p.WaitForExit();
return $"{output}\n{error}";
}
catch (Exception e)
{
return e.ToString();
}
}
}

public class SqlServerInfo
Expand Down
2 changes: 2 additions & 0 deletions sample/KerberosDemo/KerberosDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="5.0.12" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.0.0" />
<PackageReference Include="Steeltoe.Bootstrap.Autoconfig" Version="3.1.3" />
<PackageReference Include="Steeltoe.Management.EndpointCore" Version="3.1.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>

Expand Down
4 changes: 3 additions & 1 deletion sample/KerberosDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Steeltoe.Bootstrap.Autoconfig;

namespace KerberosDemo
{
Expand All @@ -21,6 +22,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
})
.AddSteeltoe();
}
}
11 changes: 11 additions & 0 deletions sample/KerberosDemo/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
}
},
"AllowedHosts": "*",
"Management": {
"Endpoints": {
"Actuator": {
"Exposure": {
"Include": [
"*"
]
}
}
}
}
// "ConnectionStrings": {
// "SqlServer": "Server=ad.almirex.com;Database=master;Trusted_Connection=True;TrustServerCertificate=True"
// }
Expand Down
4 changes: 2 additions & 2 deletions sample/KerberosDemo/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
applications:
- name: KerberosDemo
path: bin/Debug/net5.0/linux-x64/publish
path: bin/Debug/net5.0/publish
memory: 512M
health-check-type: none
buildpacks:
- https://github.com/macsux/kerberos-buildpack/releases/download/v1.0.6/KerberosBuildpack-linux-x64-v1.0.6.zip
- https://github.com/macsux/kerberos-buildpack/releases/download/v1.0.7/KerberosBuildpack-linux-x64-v1.0.7.zip
- dotnet_core_buildpack
env:
KRB5_KDC: dc1.macsux.com
Expand Down
4 changes: 2 additions & 2 deletions src/KerberosBuildpack/BuildpackBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ protected void DoApply(AbsolutePath buildPath, AbsolutePath cachePath, AbsoluteP
var startupScriptName = $"{index:00}_{nameof(KerberosBuildpack)}_startup.sh";
var startupScript = $"#!/bin/bash\n$DEPS_DIR/{index}/{prestartCommand} {index}\n";
File.WriteAllText(Path.Combine(profiled,startupScriptName), startupScript);
InstallStartupEnvVars(profiled, index, false);
GetEnvScriptFile(profiled, index, true); // causes empty env file to be created so it can (potentially) be populated with vars during onstart hook
}
InstallStartupEnvVars(profiled, index, false);
GetEnvScriptFile(profiled, index, true); // causes empty env file to be created so it can (potentially) be populated with vars during onstart hook

}

Expand Down

0 comments on commit 014b134

Please sign in to comment.