0

I'm trying to develop a procedure in HANA that activates and deactivates a special database user and logs everything in this regard.

I like that the user always has the same initial password after activation - but is forced to change it when he logs in.

Do you have an idea of how I can implement this?

Currently, I am failing due to the password policy. I don't want to touch this policy and it says that the last 5 passwords are not allowed to be used.

Is there a way to give the user any password five times with a random function and the sixth time the default password?

Any feedback regarding the implementation, improvements, and further ideas is welcome.

Thanks in advance.

Toni
  • 1
  • 1

2 Answers2

0

You asked for feedback:

  • working around security policies that presumably have been set on purpose is not a good idea

  • implementing the logging yourself does not seem like a good idea either. SAP HANA provides auditing as a feature. Instead of writing logging code, consider setting up an appropriate audit policy

  • the idea of having a user with the same post-activation password that then needs to be changed is rather insecure. It's literally like leaving the key to your house right outside the door together with the instruction to swap the locks after entering. This is not how security works.

  • there is no need to manually COMMIT after ALTER USER

  • seeing that your code expects a whole list of users it looks like one potential usage scenario for this could be test or training users. If that is the case, the recommendation is to not bother with user re-activation at all.
    Either create eternal single-use accounts (e.g. TRAIN01-2020-01 ... TRAIN01-2020-01) or drop and recreate the users whenever a new set of users should use the accounts. This will avoid the need to "dance around" a 5 last passwords policy altogether.

  • PLEASE choose names for the procedures that make it clear, what the code is supposed to do. "START"/"STOP" has no relation to the code.

  • What is the purpose of keeping the state of de-activated accounts in the table? HANA keeps track of the current state incl. the deactivation timestamp automatically.

  • coming back to the idea of "simply using 5 random passwords" before setting the password to a well-known one: nothing in your code handles the case when the random password has been produced before. Also: usually the password policies also limit how often per day passwords can be changed - precisely to avoid strategies like this.

Hope this helps, nevertheless.

Lars Br.
  • 9,949
  • 2
  • 15
  • 29
  • Thanks for your detailed feedback, Lars. I agree with you on all points and will improve them. Do you by chance know how I pass values, which I generate by other procedures? I ask because I have found a password generator (http://www.intellify.de/blog/sap-hana/sap-hana-password-generator-procedure) which works pretty well. – Toni Sep 17 '20 at 10:49
  • The referenced procedure returns the password via the default result set. This cannot be used in other procedures, but you can easily either turn this into a function or provide a return parameter. – Lars Br. Sep 17 '20 at 11:03
0

I think it's bad to give any advice when someone tries to reinvent the wheel and workaround security policies, because someone else will need to support this monstrous solution.

But anyway, why you require any random password if finally you'll set a desired one? Just loop over 6 predefined passwords and set them one after another. For auditing you can create an audit policy for that users and use standard supported tools to track changes.

EDIT: This is the code about "loop over 6 predefined passwords":

do( in iv_base_pwd nvarchar(20) => 'qwe')
begin
  declare lv_pwd_count int;
  declare lv_i int := 0;
  
  select value
    into lv_pwd_count
  from m_password_policy
  where property = 'last_used_passwords';
  
  while lv_i <= lv_pwd_count do
    execute immediate 'alter user myuser password ''' || iv_base_pwd || '_' || lv_i | '''';
    lv_i := lv_i + 1;
  end while;

  execute immediate 'alter user myuser password ''' || iv_base_pwd || '''';;
end;
astentx
  • 6,393
  • 2
  • 16
  • 25
  • Since I am really limited in programming, this was my first approach. I am aware of the security violations and I am well informed about the HANA-side security mechanisms. The password guideline says that not only the last PW cannot be used, but also the last ones. Anyway, I'll have a look at how to optimize the previous approach. – Toni Oct 04 '20 at 14:22
  • @Toni indeed. But you don't need any random sequence at all if finally you use predefined pwd. Just create a set of size last_used_passwords + 1 and loop over it to skip last N passwords. Then set the one you wish. I've updated an answer. – astentx Oct 09 '20 at 08:44