Skip to content

Commit 014b134

Browse files
committed
Fix env vars not being properly setup by buildpack
1 parent ba189cc commit 014b134

File tree

9 files changed

+105
-14
lines changed

9 files changed

+105
-14
lines changed

.nuke/build.schema.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"Publish",
8181
"PublishSample",
8282
"Release",
83+
"Restore",
8384
"Supply"
8485
]
8586
}
@@ -99,6 +100,7 @@
99100
"Publish",
100101
"PublishSample",
101102
"Release",
103+
"Restore",
102104
"Supply"
103105
]
104106
}

README.MD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ applications:
2525
- dotnet_core_buildpack
2626
env:
2727
KRB5_KDC: ad.almirex.com
28-
KRB_SERVICE_ACCOUNT: iwaclient@almirex.dc
28+
KRB_SERVICE_ACCOUNT: iwaclient@almirex.dc # MUST BE in format sAMAccountName@KerberosRealm
2929
KRB_PASSWORD: P@ssw0rd
3030

3131
```
3232

33+
3334
** 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.
3435

3536
## How it works

build/Build.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public bool IsGitHubRepository
9292
Target Publish => _ => _
9393
.Description("Packages buildpack in Cloud Foundry expected format into /artifacts directory")
9494
.After(Clean)
95-
.DependsOn(PublishSample)
95+
.DependsOn(PublishSample, Restore)
9696
.Executes(() =>
9797
{
9898
var workDirectory = TemporaryDirectory / "pack";
@@ -111,6 +111,7 @@ public bool IsGitHubRepository
111111
.SetFramework(Framework)
112112
.SetRuntime(Runtime)
113113
.EnableSelfContained()
114+
.EnableNoRestore()
114115
.SetAssemblyVersion(GitVersion.AssemblyVersion)
115116
.SetFileVersion(GitVersion.AssemblyFileVersion)
116117
.SetInformationalVersion(GitVersion.AssemblyInformationalVersion)
@@ -138,13 +139,25 @@ public bool IsGitHubRepository
138139
Logger.Block(ArtifactsDirectory / PackageZipName);
139140
});
140141

142+
Target Restore => _ => _
143+
.Executes(() =>
144+
{
145+
DotNetRestore(c => c
146+
.SetProjectFile(Solution));
147+
148+
DotNetPublish(c => c
149+
.SetProject(RootDirectory / "sample" / "Samples.sln"));
150+
});
151+
141152
Target PublishSample => _ => _
153+
.DependsOn(Restore)
142154
.Executes(() =>
143155
{
144156
var demoProjectDirectory = RootDirectory / "sample" / "KerberosDemo";
145-
// DotNetPublish(c => c
146-
// .SetProject(demoProjectDirectory / "KerberosDemo.csproj")
147-
// .SetConfiguration("DEBUG"));
157+
DotNetPublish(c => c
158+
.SetProject(demoProjectDirectory / "KerberosDemo.csproj")
159+
.EnableNoRestore()
160+
.SetConfiguration("DEBUG"));
148161
var publishFolder = demoProjectDirectory / "bin" / "Debug" / "net5.0" / "publish";
149162
var manifestFile = publishFolder / "manifest.yml";
150163
var manifest = File.ReadAllText(manifestFile);

sample/KerberosDemo/Controllers/HomeController.cs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
4-
using System.Net;
5-
using System.Net.NetworkInformation;
5+
using File = System.IO.File;
66
using System.Net.Sockets;
77
using System.Security.Claims;
88
using System.Text;
@@ -132,8 +132,68 @@ public async Task<string> TestKDC(string kdc)
132132
return $"Failed connection test to {kdc} on port 88\n{e}";
133133
}
134134
}
135-
136-
135+
136+
[HttpGet("/diag")]
137+
public async Task<string> Diagnostics()
138+
{
139+
var sb = new StringBuilder();
140+
VerifyEnvVar("KRB5_CONFIG");
141+
VerifyEnvVar("KRB5CCNAME");
142+
VerifyEnvVar("KRB5_KTNAME");
143+
144+
var krb5Conf = Environment.GetEnvironmentVariable("KRB5_CONFIG");
145+
if (!string.IsNullOrEmpty(krb5Conf))
146+
{
147+
sb.AppendLine($"[{krb5Conf} content]");
148+
sb.AppendLine(System.IO.File.ReadAllText(krb5Conf));
149+
}
150+
151+
void VerifyEnvVar(string var)
152+
{
153+
var varValue = Environment.GetEnvironmentVariable(var);
154+
sb.AppendLine($"{var}={varValue}");
155+
if (!string.IsNullOrEmpty(varValue))
156+
{
157+
var fileExists = System.IO.File.Exists(varValue);
158+
sb.AppendLine($"{varValue} = {(fileExists ? "exists" : "missing")}");
159+
}
160+
}
161+
162+
return sb.ToString();
163+
}
164+
165+
[HttpGet("/run")]
166+
private string Run(string command)
167+
{
168+
var commandSegments = command.Split(" ");
169+
var processName = commandSegments[0];
170+
var args = command[1..];
171+
// Start the child process.
172+
try
173+
{
174+
175+
176+
Process p = new Process();
177+
// Redirect the output stream of the child process.
178+
p.StartInfo.UseShellExecute = false;
179+
p.StartInfo.RedirectStandardOutput = true;
180+
p.StartInfo.FileName = processName;
181+
p.StartInfo.Arguments = string.Join(" ", args);
182+
p.Start();
183+
// Do not wait for the child process to exit before
184+
// reading to the end of its redirected stream.
185+
// p.WaitForExit();
186+
// Read the output stream first and then wait.
187+
var output = p.StandardOutput.ReadToEnd();
188+
var error = p.StandardError.ReadToEnd();
189+
p.WaitForExit();
190+
return $"{output}\n{error}";
191+
}
192+
catch (Exception e)
193+
{
194+
return e.ToString();
195+
}
196+
}
137197
}
138198

139199
public class SqlServerInfo

sample/KerberosDemo/KerberosDemo.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<PackageReference Include="Dapper" Version="2.0.123" />
99
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="5.0.12" />
1010
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.0.0" />
11+
<PackageReference Include="Steeltoe.Bootstrap.Autoconfig" Version="3.1.3" />
12+
<PackageReference Include="Steeltoe.Management.EndpointCore" Version="3.1.3" />
1113
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
1214
</ItemGroup>
1315

sample/KerberosDemo/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.Hosting;
88
using Microsoft.Extensions.Logging;
9+
using Steeltoe.Bootstrap.Autoconfig;
910

1011
namespace KerberosDemo
1112
{
@@ -21,6 +22,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
2122
.ConfigureWebHostDefaults(webBuilder =>
2223
{
2324
webBuilder.UseStartup<Startup>();
24-
});
25+
})
26+
.AddSteeltoe();
2527
}
2628
}

sample/KerberosDemo/appsettings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
}
88
},
99
"AllowedHosts": "*",
10+
"Management": {
11+
"Endpoints": {
12+
"Actuator": {
13+
"Exposure": {
14+
"Include": [
15+
"*"
16+
]
17+
}
18+
}
19+
}
20+
}
1021
// "ConnectionStrings": {
1122
// "SqlServer": "Server=ad.almirex.com;Database=master;Trusted_Connection=True;TrustServerCertificate=True"
1223
// }

sample/KerberosDemo/manifest.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
applications:
33
- name: KerberosDemo
4-
path: bin/Debug/net5.0/linux-x64/publish
4+
path: bin/Debug/net5.0/publish
55
memory: 512M
66
health-check-type: none
77
buildpacks:
8-
- https://github.com/macsux/kerberos-buildpack/releases/download/v1.0.6/KerberosBuildpack-linux-x64-v1.0.6.zip
8+
- https://github.com/macsux/kerberos-buildpack/releases/download/v1.0.7/KerberosBuildpack-linux-x64-v1.0.7.zip
99
- dotnet_core_buildpack
1010
env:
1111
KRB5_KDC: dc1.macsux.com

src/KerberosBuildpack/BuildpackBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ protected void DoApply(AbsolutePath buildPath, AbsolutePath cachePath, AbsoluteP
7878
var startupScriptName = $"{index:00}_{nameof(KerberosBuildpack)}_startup.sh";
7979
var startupScript = $"#!/bin/bash\n$DEPS_DIR/{index}/{prestartCommand} {index}\n";
8080
File.WriteAllText(Path.Combine(profiled,startupScriptName), startupScript);
81-
InstallStartupEnvVars(profiled, index, false);
82-
GetEnvScriptFile(profiled, index, true); // causes empty env file to be created so it can (potentially) be populated with vars during onstart hook
8381
}
82+
InstallStartupEnvVars(profiled, index, false);
83+
GetEnvScriptFile(profiled, index, true); // causes empty env file to be created so it can (potentially) be populated with vars during onstart hook
8484

8585
}
8686

0 commit comments

Comments
 (0)