Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jnWla-0005fE-07 for pgsql-admin@arkaria.postgresql.org; Tue, 23 Jun 2020 00:26:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jnWlY-0006Dx-EO for pgsql-admin@arkaria.postgresql.org; Tue, 23 Jun 2020 00:26:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jnWlY-0006Dp-4L for pgsql-admin@lists.postgresql.org; Tue, 23 Jun 2020 00:26:20 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jnWlR-0005HI-IE for pgsql-admin@lists.postgresql.org; Tue, 23 Jun 2020 00:26:18 +0000 Received: by mail-pf1-x432.google.com with SMTP id j12so7006193pfn.10 for ; Mon, 22 Jun 2020 17:26:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:subject:in-reply-to:message-id:date :mime-version; bh=zlNRH1VlvtnduOModAzlQ8m3kNWsMvJtsHz8CvXCUJw=; b=psvV7nAV7t6m1gqQMEcydbUZ1smWj4/RKlMvGL1lUhIcauAEeJL0PcUf76Ml/wjoD+ j2zOwaUv+DrBA1AA0fJ4i8ltnzoQyzYPo/YUwpt67Hpfk8Y6fGwt2pxYAtzWhf4aVLoT s+CDXrUd5xHLrOqFvU0Ak3KqR4UKSBaIOrk2GUeNwyXcOoT71wPz07blLe/ao9J3kMun wMTRxztWtx93oKWvSboii+NGTgWc9WNV1rBX5Lu3GxI0wa5yRhbYMCNJrp80J2xiSIx1 xhHxQMcum24f74f90/a7hbPoyCTFjkvFj0UesWLxnegMgJF3IMosg2QOxctO4wlV4uRM /B1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:subject :in-reply-to:message-id:date:mime-version; bh=zlNRH1VlvtnduOModAzlQ8m3kNWsMvJtsHz8CvXCUJw=; b=ZhotOttdqwH8WuQ/8ZNYTkKnR3XQjUicrDJNpIlRsnmMXZvyYFQkBRws1UyYtw2Usb 88Qb3U8ZF0i6jOP97ZxoXx4Te7GRjzk9Xo8664fCXpFle/W+cYxGRhXSNE9PbKIU5zvJ fL+AP4i1QxmIfR6rR+2DmXTH4NuJNCBEoncR7od75mUtdnjTfpOq0s1FAVHA9JZVhF1h tdZoHXGcZ5rUSSMHCdA9uhbnLJyH5eskl5jvrrdQzdzopF3GXvqi/DhZ+wRDfPijACr1 whyi4y5b6IFdTD2egeArEulFY2mZr3i4EhgvLINq9NoXgH8Urh8S6UQpU/bGbfohujAV XDmA== X-Gm-Message-State: AOAM532rrJhtmR/6AMbF+yubSvibF/WoLzJEspMrLaK2GyGqfh1lshRG vI+ORS6aa0oRwAs4lvTp9tY07FnyJjU= X-Google-Smtp-Source: ABdhPJzKATleUVVa429dbqFDjjaHrXCvHudBLoERBFTObPHWwafny9q96RRE4YIMHTf0r+d5mTztIQ== X-Received: by 2002:a65:4149:: with SMTP id x9mr15600594pgp.424.1592871972188; Mon, 22 Jun 2020 17:26:12 -0700 (PDT) Received: from tim-desktop (203-173-27-74.dyn.iinet.net.au. [203.173.27.74]) by smtp.gmail.com with ESMTPSA id d2sm10795024pfc.1.2020.06.22.17.26.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2020 17:26:11 -0700 (PDT) References: User-agent: mu4e 1.5.3; emacs 27.0.91 From: Tim Cross To: pgsql-admin@lists.postgresql.org Subject: Re: scram-sha-256 encrypted password in pgpass In-reply-to: Message-ID: <87mu4ud3vj.fsf@gmail.com> Date: Tue, 23 Jun 2020 10:26:08 +1000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Pavan Kumar writes: > Adrian, David, > > Thank you so much for the quick response. > > What would be the point of storing the encrypted password instead of the > plaintext one? > As per our organization security policies, we can 't keep any passwords in > plain text format. > I am working on postgres + pgbouncer setup, tested pgbouncer 1.14 where we > have support to use encrypted password in userlist,txt file. I am > surprised why pgpass is not supporting encrypted passwords. > > I suspect part of the issue is that the only way you can do this is to encrypt the .pgpass file. Part of the problem is that people think that scram-sha-256 is encrypting passwords when in fact it is a one-way hash - you cannot derive the original password from the hash. A hash only goes in one direction. Encryption on the other hand is generally 2-way. You have a key which encrypts the data and a key which decrypts it back to plain text. So, having protected passwords in .pgpass is not as simple as just copying the scram-sha-256 hash value into the password field. PG needs to hash the provided value and compare it to the stored scram-sha-256 value to know the original password was supplied and there is no way to get the original password from the sha'd version. If you need to use a password in a command line scenario (i.e. with a script), then one way to get around the issue of not storing plain text passwords is to use GPG. The basic model is - Create a GPG key and store it in a secure place, such as a keystore - Use that GPG key to encrypt your password in a file e.g. my-secret.gpg - In your script, you can have something like PWD = `gpg -q --for-your-eyes-only --no-tty -d ~/.secure/my-secret.gpg` The above line will use the key you stored in the keyring/keystore to encrypt my-secret.gpg to decrypt that file and send the contents to stdout, which in turn gets assigned to the PWD variable. Using GPG is a pretty reliable and portable solution. These days, there are specific programs, essentially password safes, designed for sys admin purposes which essentially do the same thing, but at a higher level. For larger organisations with lots of sys admins and complex security policies etc, investment in one of these 'enterprise' solutions is usually a good idea (provides lots of other features, such as never allowing sys admins to actually know passwords/keys so that when one leaves, you don't have to go around changing credentials on the all the systems they may have had access to. They work in a similar way to how things like lastpass or password1 work in user land). I suspect it is unlikely you will ever see a .pgpass solution which supports encryption. There are just too many 'chicken and egg' problems - you need a key to encrypt the .pgpass file, but now you need to store the key securely. Problem made more difficult because different platforms all do this in different ways and with different levels of sophistication. While it could be done, the amount of work required is probably more than the desire for anyone to implement it (not a big enough itch).