0

In my .net core web API , I'm using OpenHtmlToPdf nuget package for rendering HTML documents to PDF format. The implementation working fine locally but not in the server production K8 environment. I'm getting following message from the logs.

Permission denied

I did several workaround and was able to find out, a lib called OpenHtmltoPdf uses Path.GetTempPath() and Guid.NewGuid() to create a temp file. seems above permission denied error occurs when it trying to access that temp path. this is code snippet from the OpenHtmltoPdf lib. OpenHtmlToPdf git repo

//inside TemporaryPdf class
public static string TemporaryFilePath() 
{
  return Path.Combine(Path.GetTempPath(), "OpenHtmlToPdf", TemporaryPdf.TemporaryFilename());
}

private static string TemporaryFilename()
{
  return Guid.NewGuid().ToString("N") + ".pdf";
}

I just added following line to my application to determine the temp path and its returns File Path is: /tmp/.

Console.WriteLine("File path is:" + Path.GetTempPath());

But in the Kubernetes pods have rwx permissions for /tmp/ folder for the all users.

enter image description here

My problem is, after deploying the API, do the NuGet packages use a separate temp path? So how exactly identify it?

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
  • how about trying `File.Create(TemporaryPdf.TemporaryFilePath())`, do determine it is really this line of code causing issue? – Lei Yang Feb 23 '22 at 08:14
  • For Path.GetTempPath() - "The path specified by the TMPDIR environment variable. If the path is not specified in the TMPDIR environment variable, the default path /tmp/ is used." . Try to set another "tmp" path by overwriting the environment variable. – D A Feb 23 '22 at 08:15
  • @LeiYang I think you mean, add `Console.WriteLine("File path is " + Path.GetTempPath())` for the library? yeah I tried that too. but I couldn't determine. – Sachith Wickramaarachchi Feb 23 '22 at 08:25
  • no. i mean you write code to create the file, of course printing the path is also useful. – Lei Yang Feb 23 '22 at 08:30
  • NuGet packages don't exist after compilation, nor do they affect the deployment environment. Since you want a temporary filename use [Path.GetTempFileName](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.gettempfilename?view=net-6.0) and don't bother about the name or extension. You can specify a different name when sending the file to the caller or copying it to its final location. – Panagiotis Kanavos Feb 23 '22 at 09:32
  • Are you looking at the host or pod tmp folder? – Panagiotis Kanavos Feb 23 '22 at 09:33
  • @PanagiotisKanavos pod `tmp` folder – Sachith Wickramaarachchi Feb 23 '22 at 09:35
  • @PanagiotisKanavos No actually I don't required TempFileName. just need to know the which folder uses by `openhtmltopdf` lib . the above code snippet from the `openhtmltopdf` lib. – Sachith Wickramaarachchi Feb 23 '22 at 09:38
  • 1
    Did you already check what TemporaryFilePath returns to be sure the path is what you are expecting and writeable? – Starbax Feb 23 '22 at 09:41
  • @SachithWickramaarachchi NuGet on the build machine doesn't affect the deployed machines. Try checking that path's permissions. You can use the `Mono.Posix` package and the [UnixDirectoryInfo](https://github.com/mono/mono/blob/main/mcs/class/Mono.Posix/Mono.Unix/UnixDirectoryInfo.cs) class to find the actual permissions of that path. – Panagiotis Kanavos Feb 23 '22 at 09:43

0 Answers0