顯示具有 Trouble Shooting 標籤的文章。 顯示所有文章
顯示具有 Trouble Shooting 標籤的文章。 顯示所有文章

2020年12月16日 星期三

[Dockerfile] Trouble shooting - cannot get environment variables and permission denied on Entrypoint

 

 Docker   Dockerfile   sudo  

 

 

Problem


 

We have a Dockerfile that will switch user to run a shell script on ENTRYPOINT, however we got the 2 problems,

 

1.  Cannot get the environment variables in the shell script.

2.  Get permission denied after we preserve the environment variables when sudo user.

 

Here are the sample files,

 

Dockerfile

FROM postgres:11

ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD xxxxx

# Add user: postgres
RUN sudo adduser postgres sudo

EXPOSE 5432
ENTRYPOINT ["/bin/sh""-c""sudo -u postgres sh callback.sh"]


 

Shell script (callback.sh)

#!/bin/bash echo POSTGRES_USER=$POSTGRES_USER
echo POSTGRES_PASSWORD=$POSTGRES_PASSWORD
echo "some logs" >> ~/my_log

The container from the above Dockerfile could not get the environment variables $POSTGRES_USER and $POSTGRES_PASSWORD, and finally output,

 

POSTGRES_USER=

POSTGRES_PASSWORD=

 

And the log file will be located at /var/lib/postgresql/my_log.

 

 

Environment


 

Docker desktop 2.3.0.3

 

 

 

Solution


 

Preserving environment variables

 

The problem was due to not preserving environment variables when switching to other user.

 

So we have to preserve the existing environment variables by adding the argument -U when sudo. (See sudo manual)

 

-E, --preserve-env: Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to preserve the environment.

 

So we must update the following line of Dockerfile,

ENTRYPOINT ["/bin/sh""-c""sudo -E -u postgres sh callback.sh"]

 

But it caused the second problem: Permission denied when writing log to ~/my_log.

The output became as following,

 

POSTGRES_USER=postgres

POSTGRES_PASSWORD=xxxxx

callback.sh: 3: callback.sh: cannot create /root/my_log: Permission denied

 

 

As you can see the ~/ is changed to /root/ but not /var/lib/postgresql/, so that the user: postgres could not have the permission on ~/.

 

Finally I solved the problems by updating the Dockerfile as following,

 

 

Dockerfile

FROM postgres:11

ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD xxxxx

 
# Add user: postgres
RUN sudo adduser postgres sudo

EXPOSE 5432
ENTRYPOINT ["/bin/sh""-c""sudo -E -u postgres sh callback.sh"]

# Or use this # ENTRYPOINT ["/bin/sh", "-c", "su --preserve-environment - postgres callback.sh'"]


 

Shell script (callback.sh)

#!/bin/bash echo POSTGRES_USER=$POSTGRES_USER echo POSTGRES_PASSWORD=$POSTGRES_PASSWORD echo "some logs" >> /var/lib/postgresql/my_log

 

2019年4月26日 星期五

[Sourcetree] Delete passwd


 Sourcetree   Windows credential


Recently my windows account had been locked frequently and don’t know which application used the wrong password of windows credential.
Finally I found out the Sourcetree had kept the Windows credential (ID/PWD) in the following  location,

~\AppData\Local\Atlassian\SourceTree\passwd

Ex.
C:\Users\karatejb\AppData\Local\Atlassian\SourceTree\passwd





The solution is deleting the file: passwd, and restart Sourcetree and a  popup will ask for the new credential.


Reference






2017年1月27日 星期五

[Sharepoint] People picker cannot recoginize domain user

 Sharepoint    AD   People picker


▌Issue


Issue: The people picker in Sharepoint could not recognize the some domain users.


How to solve it


Find the administration cmd tool of SharePoint Server

First, we have to find the stsadm.exe tool, it’s located in

C:\Program Files\Common Files\Microsoft Shared\web server extensions\{version#}\bin


Search the available domain users

Use the following command to analyze the AD users in certain domain.

$> stsadm -o setproperty -pn peoplepicker-searchadforests -pv "{domain name}" -url http://sharepoint-portal/


Done :)


Reference




2016年8月4日 星期四

[Trouble shooting][angular-ui-bootstrap] datepicker-popup focus not works after date selected

 #angular-ui   #datepicker-popup   #focus 

Problem



The datepicker-popup cannot be focused after selecting a date on version 1.3.3 .


Trouble shooting


Fix the js


Open the angular-ui-bootstrap javascript (ex. ui-bootstrap-tpls.js)

You will find there is a listener for 'uib:datepicker.focus'

// Listen for focus requests from popup directive
$scope.$on('uib:datepicker.focus', focusElement);

It will set the focus to the original directive, however it’s not works on IE. (Tested in IE11)
So we have to skip broadcasting this event when we select any date on the popup directive.

The following function will be found on controller : UibDatepickerController
Skip the last line for broadcasting, and this problem will be solved. This way was tested fine on IE11 and Chrome.

$scope.select = function(date) {
    if ($scope.datepickerMode === self.minMode) {
      var dt = ngModelCtrl.$viewValue ? dateParser.fromTimezone(new Date(ngModelCtrl.$viewValue), ngModelOptions.timezone) : new Date(0, 0, 0, 0, 0, 0, 0);
      dt.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
      dt = dateParser.toTimezone(dt, ngModelOptions.timezone);
      ngModelCtrl.$setViewValue(dt);
      ngModelCtrl.$render();
    } else {
      self.activeDate = date;
      setMode(self.modes[self.modes.indexOf($scope.datepickerMode) - 1]);

      $scope.$emit('uib:datepicker.mode');
    }

    //$scope.$broadcast('uib:datepicker.focus');  === > Don’t run this!
};






2016年6月25日 星期六

[javascript] location.origin not work on IE

 javascript    trouble-shooting    IE  



▌Problem


window.location.origin not works on some IE.



How to solve it


js

Use window.location.protocol, window.location.hostname, window.location.port to replace window.location.origin.

if (!window.location.origin) {           
window.location.origin = window.location.protocol + '//' + window.location.hostname + (window.location.port ? (':' + window.location.port) : '');
        }

var redirectUrl = window.location.origin.concat("/Area/Control/Edit", "?id=" + id);


2016年5月21日 星期六

[Entity Framework 6] Attach and Detach

.NET   Entity Framework    ORM


雖說自己在先前的文章:
曾提到用不同的DbContext來操作Entity的小陷阱。

結果一年後的今天還是踩到了這個雷 ~_~|| (而且完全沒有Exception message …)


Reproduce the problem

在以下程式碼中,當Entity需要傳到不同的方法操作時,如果是不同的DbContext,會造成實際上已更新物件,但資料庫卻未更新的情況。

//In Method 1
using (var daoService = new BloodTypeService<BloodType>(new TmsDbContext()))
{
    bloodType = daoService.GetAll().ToList().ElementAt(0);
    LogUtility.Logger.Debug(bloodType.Name);
}

//In Method 2
using (var daoService = new BloodTypeService<BloodType>(new TmsDbContext()))
{
    bloodType.Name = "AAA";
    daoService.Update(bloodType); //包含SaveChanges()

    var bloodTypeReQuery = daoService.GetAll().ToList().ElementAt(0);
    LogUtility.Logger.Debug(bloodTypeReQuery.Name);
}

上面的執行結果是 
A
A

也就是沒有更新到實體資料庫

Refinement 1


var dbContext = new TmsDbContext();

//In Method 1
using (var daoService = new BloodTypeService<BloodType>(dbContext))
{
      bloodType = daoService.GetAll().ToList().ElementAt(0);
      LogUtility.Logger.Debug(bloodType.Name);
}

//In Method 2
using (var daoService = new BloodTypeService<BloodType>(dbContext))
{
      bloodType.Name = "AAA";
      daoService.Update(bloodType); //包含SaveChanges()

      var bloodTypeReQuery = daoService.GetAll().ToList().ElementAt(0);
      LogUtility.Logger.Debug(bloodTypeReQuery.Name);
}

上面的兩個方法共用同一個DbContext後,就可以成功更新該筆資料庫的資料。

Output結果為
A
AAA

Refinement 2

也可以使用Detach & attach的方式來修正程式碼。

BloodType bloodType = null;

//In Method 1
var dbContext1 = new TmsDbContext();
using (var daoService = new BloodTypeService<BloodType>(dbContext1))
{
                bloodType = daoService.GetAll().ToList().ElementAt(0);

                //Detach method 1 : Using ObjectContext
                ObjectContext objContext1 = ((IObjectContextAdapter)dbContext1).ObjectContext;
                objContext1.Detach(bloodType);
                //Detach method 2 : Using EntityState to detach
                //dbContext1.Entry(bloodType).State = EntityState.Detached;

                LogUtility.Logger.Debug(bloodType.Name);
}

//In Method 2
var dbContext2 = new TmsDbContext();
using (var daoService = new BloodTypeService<BloodType>(dbContext2))
{
                //Attach method 1 : Using DbSet.Attach()
                dbContext2.Set(typeof(BloodType)).Attach(bloodType);
                //Attach method 2 : Using EntityState to attach (If the DAO is modified before attaching it, must use this way)
                //dbContext2.Entry(bloodType).State = EntityState.Modified;

                bloodType.Name = "AAA";
                daoService.Update(bloodType); //包含SaveChanges()

                var bloodTypeReQuery = daoService.GetAll().ToList().ElementAt(0);
                LogUtility.Logger.Debug(bloodTypeReQuery.Name);
}



2016年5月2日 星期一

[CSS] Font Awesome 404 issue

 CSS   Font Awesome   404 error



▌Background


開發專案中使用到Font Awesome CSS模組。 這個CSS是以自訂字型來做為CSS  content顯示的圖片。
例如下圖的上面四個按鈕,其實是Font Awesome的字型:



在未正確設定Web application時,會出現無法顯示字型和以下的404錯誤:


如果要正確顯示,必須進行以下設定。

Trouble shooting

CSS and Font

Font Awesome的字型加入到專案,本例是置於 /font/



修改font-awesome.min.css 的字型路徑: (預設是 ‘../fonts/….’)




Web config

WebConfig加入以下設定讓IIS支援MIME type : font-woff*

<system.webServer>
    <staticContent>
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
    </staticContent>
  </system.webServer>


Reference