简体   繁体   中英

Unable to change some js/html file content on container start. getting access denied exception

I have a case where i need to inject some Envrionment variable values in compiled vuejs js/html file which resides inisde my .net core 3.1 application. I have put the content changing logic on startup.cs in Configure method. I am getting Access denied exception My code is

private void ReplaceEnvironmentVariableWithVal()
        {
            try
            {
                string apiurlToken = "VUE_APP_BEAPI_URL";
                string relevantPath = "VUE_APP_RELEVANTPATH";
                string path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "clientapp", "dist", "js");
                Console.WriteLine($"{apiurlToken} env value is {GetEnvVariable(apiurlToken)}");
                Console.WriteLine($"{relevantPath} env value is {GetEnvVariable(relevantPath)}");
                Console.WriteLine("Application Root Path of js files " + path);
                DirectoryInfo dir = new DirectoryInfo(path);
                Dictionary<string, string> dict = new Dictionary<string, string>();
                if (dir != null)
                {
                    foreach (var file in dir.GetFiles())
                    {
                        if (file.Extension == "js" || file.Extension == ".js")
                        {
                            var fileContent = File.ReadAllText(file.FullName);
                            if (!String.IsNullOrWhiteSpace(fileContent))
                            {

                                if (fileContent.Contains(apiurlToken))
                                {
                                    string variableValue = GetEnvVariable(apiurlToken);
                                    if (!String.IsNullOrWhiteSpace(variableValue))
                                    {
                                        fileContent = fileContent.Replace(apiurlToken, variableValue);
                                        dict[file.FullName] = fileContent;
                                    }

                                }

                                if (fileContent.Contains(relevantPath))
                                {
                                    string variableValue = GetEnvVariable(relevantPath);
                                    //if (!String.IsNullOrWhiteSpace(variableValue))
                                    {
                                        fileContent = fileContent.Replace(relevantPath, variableValue);
                                        dict[file.FullName] = fileContent;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Application Root Path of js files not exists");
                }
              
                foreach (KeyValuePair<string, string> entry in dict)
                {
                    Console.WriteLine("Updating " + entry.Key);
                    using (var fs = new FileStream(entry.Key, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                    {
                        using var sr = new StreamWriter(fs);
                        sr.Write(entry.Value);
                    }
                    Console.WriteLine("Update Completed " + entry.Key);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in ReplaceEnvironmentVariableWithVal:" + ex.Message);
            }
        }

I have achieve the writing of files my giving the user right's. i have log the user under which application was running and it was default. then added following line in my docker file COPY --chown=default:default . .

which basically given right's to the user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM