-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update last_used timestamp for SSH keys
Update the last_used timestamp to the current time whenever an SSH key is used to either generate a token or query exec permissions on a Lagoon environment. The timestamp is updated every time the key is used, regardless of whether or not the permission check or token generation succeeds. Timestamps are converted and stored in UTC.
- Loading branch information
Showing
9 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package lagoondb_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/alecthomas/assert/v2" | ||
"github.com/uselagoon/ssh-portal/internal/lagoondb" | ||
) | ||
|
||
func TestLastUsed(t *testing.T) { | ||
var testCases = map[string]struct { | ||
fingerprint string | ||
used time.Time | ||
usedString string | ||
expectError bool | ||
}{ | ||
"right time": { | ||
fingerprint: "SHA256:yARVMVDnP2B2QzTvE8eSs5ZZlkZEoMFEIKjtYv1adfU", | ||
used: time.Unix(1719825567, 0), | ||
usedString: "2024-07-01 09:19:27", | ||
expectError: false, | ||
}, | ||
"wrong time": { | ||
fingerprint: "SHA256:yARVMVDnP2B2QzTvE8eSs5ZZlkZEoMFEIKjtYv1adfU", | ||
used: time.Unix(1719825567, 0), | ||
usedString: "2024-07-01 17:19:27", | ||
expectError: true, | ||
}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(tt *testing.T) { | ||
// set up mocks | ||
mockDB, mock, err := sqlmock.New() | ||
assert.NoError(tt, err, name) | ||
mock.ExpectExec( | ||
`UPDATE ssh_key `+ | ||
`SET last_used = (.+) `+ | ||
`WHERE key_fingerprint = (.+)`). | ||
WithArgs(tc.usedString, tc.fingerprint). | ||
WillReturnResult(sqlmock.NewErrorResult(nil)) | ||
// execute expected database operations | ||
db := lagoondb.NewClientFromDB(mockDB) | ||
err = db.SSHKeyUsed(context.Background(), tc.fingerprint, tc.used) | ||
if tc.expectError { | ||
assert.Error(tt, err, name) | ||
} else { | ||
assert.NoError(tt, err, name) | ||
} | ||
// check expectations | ||
err = mock.ExpectationsWereMet() | ||
if tc.expectError { | ||
assert.Error(tt, err, name) | ||
} else { | ||
assert.NoError(tt, err, name) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package lagoondb | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
func NewClientFromDB(db *sql.DB) *Client { | ||
return &Client{db: sqlx.NewDb(db, "mysql")} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters