Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nss-pam-webapi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lukas Weichelt
nss-pam-webapi
Commits
b5bfa128
Verified
Commit
b5bfa128
authored
4 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
[Cache] Get rid of global state
parent
933870cd
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/cache.rs
+10
-18
10 additions, 18 deletions
src/cache.rs
src/nss.rs
+8
-9
8 additions, 9 deletions
src/nss.rs
src/pam.rs
+7
-5
7 additions, 5 deletions
src/pam.rs
with
25 additions
and
32 deletions
src/cache.rs
+
10
−
18
View file @
b5bfa128
...
...
@@ -15,9 +15,7 @@
use
crate
::
BASE_NAME
;
use
lazy_static
::
lazy_static
;
use
std
::
collections
::
HashMap
;
use
std
::
sync
::{
RwLock
,
RwLockReadGuard
};
use
libc
::{
geteuid
,
seteuid
,
getpwnam
,
uid_t
};
use
std
::
ffi
::{
CStr
,
CString
};
...
...
@@ -39,17 +37,19 @@ const USER_TOKEN_FILENAME: &str = "user_token.json";
pub
struct
Cache
{
user_tokens
:
HashMap
<
String
,
BasicTokenResponse
>
,
original_euid
:
uid_t
,
prefix
:
String
}
impl
Cache
{
pub
fn
new
()
->
Cache
{
pub
fn
new
(
prefix
:
&
str
)
->
Cache
{
let
euid
;
unsafe
{
euid
=
geteuid
();
};
Cache
{
user_tokens
:
HashMap
::
new
(),
original_euid
:
euid
original_euid
:
euid
,
prefix
:
prefix
.to_string
()
}
}
...
...
@@ -144,7 +144,7 @@ impl Cache {
}
}
pub
fn
load_user_token
(
&
self
,
owner
:
&
String
)
->
Option
<&
BasicTokenResponse
>
{
pub
fn
load_user_token
(
&
mut
self
,
owner
:
&
String
)
->
Option
<&
BasicTokenResponse
>
{
if
!
self
.user_tokens
.contains_key
(
owner
)
{
debug!
(
"No token for {} in memory, trying to load from file"
,
owner
);
...
...
@@ -160,7 +160,7 @@ impl Cache {
match
new_token
{
Some
(
t
)
=>
{
CACHE
.write
()
.unwrap
()
.user_tokens
.insert
(
owner
.to_string
(),
t
);
self
.user_tokens
.insert
(
owner
.to_string
(),
t
);
self
.user_tokens
.get
(
owner
)
},
None
=>
None
...
...
@@ -171,8 +171,8 @@ impl Cache {
}
}
pub
fn
save_user_token
(
&
self
,
owner
:
&
String
,
token
:
BasicTokenResponse
)
->
Result
<
(),
io
::
Error
>
{
CACHE
.write
()
.unwrap
()
.user_tokens
.insert
(
owner
.to_string
(),
token
.clone
());
pub
fn
save_user_token
(
&
mut
self
,
owner
:
&
String
,
token
:
BasicTokenResponse
)
->
Result
<
(),
io
::
Error
>
{
self
.user_tokens
.insert
(
owner
.to_string
(),
token
.clone
());
debug!
(
"Saved token for {} in memory"
,
owner
);
// Try to write user's token cache file
...
...
@@ -190,8 +190,8 @@ impl Cache {
return
res
;
}
pub
fn
delete_user_token
(
&
self
,
owner
:
&
String
)
{
CACHE
.write
()
.unwrap
()
.user_tokens
.remove
(
owner
);
pub
fn
delete_user_token
(
&
mut
self
,
owner
:
&
String
)
{
self
.user_tokens
.remove
(
owner
);
debug!
(
"Token for {} removed from memory"
,
owner
);
// Try to remove user's token cache file
...
...
@@ -225,11 +225,3 @@ fn save_json<O: Serialize>(path: PathBuf, obj: O) -> Result<(), io::Error> {
fs
::
write
(
path
,
json
)
}
lazy_static!
{
static
ref
CACHE
:
RwLock
<
Cache
>
=
RwLock
::
new
(
Cache
::
new
());
}
pub
fn
get_cache
()
->
RwLockReadGuard
<
'static
,
Cache
>
{
CACHE
.read
()
.unwrap
()
}
This diff is collapsed.
Click to expand it.
src/nss.rs
+
8
−
9
View file @
b5bfa128
...
...
@@ -19,7 +19,7 @@ use crate::config::{
get_or_error
};
use
config
::
Config
;
use
crate
::
cache
::
get_c
ache
;
use
crate
::
cache
::
C
ache
;
use
crate
::
logging
::
setup_log
;
...
...
@@ -45,7 +45,7 @@ struct PasswdDef {
}
#[derive(Deserialize)]
struct
PasswdHelper
(
#[serde(with
=
"PasswdDef"
)]
Passwd
);
fn
nss_hook_prepare
()
->
Config
{
fn
nss_hook_prepare
()
->
(
Cache
,
Config
)
{
let
conf
=
get_config
(
None
);
let
mut
log_level
=
log
::
LevelFilter
::
Error
;
...
...
@@ -54,7 +54,9 @@ fn nss_hook_prepare() -> Config {
}
setup_log
(
log_level
);
return
conf
;
let
cache
=
Cache
::
new
(
"nss"
);
return
(
cache
,
conf
);
}
fn
get_current_user
()
->
String
{
...
...
@@ -80,8 +82,7 @@ struct OidcPasswd;
impl
PasswdHooks
for
OidcPasswd
{
fn
get_all_entries
()
->
Response
<
Vec
<
Passwd
>>
{
let
conf
=
nss_hook_prepare
();
let
mut
cache
=
get_cache
();
let
(
mut
cache
,
conf
)
=
nss_hook_prepare
();
let
user
=
get_current_user
();
let
ctc
;
...
...
@@ -114,8 +115,7 @@ impl PasswdHooks for OidcPasswd {
}
fn
get_entry_by_uid
(
uid
:
libc
::
uid_t
)
->
Response
<
Passwd
>
{
let
conf
=
nss_hook_prepare
();
let
mut
cache
=
get_cache
();
let
(
mut
cache
,
conf
)
=
nss_hook_prepare
();
let
user
=
get_current_user
();
let
ctc
;
...
...
@@ -148,8 +148,7 @@ impl PasswdHooks for OidcPasswd {
}
fn
get_entry_by_name
(
name
:
String
)
->
Response
<
Passwd
>
{
let
conf
=
nss_hook_prepare
();
let
mut
cache
=
get_cache
();
let
(
mut
cache
,
conf
)
=
nss_hook_prepare
();
let
user
=
get_current_user
();
let
ctc
;
...
...
This diff is collapsed.
Click to expand it.
src/pam.rs
+
7
−
5
View file @
b5bfa128
...
...
@@ -24,11 +24,11 @@ use crate::oauth::get_access_token_password;
use
crate
::
logging
::
setup_log
;
use
crate
::
cache
::
get_c
ache
;
use
crate
::
cache
::
C
ache
;
use
pamsm
::{
PamServiceModule
,
Pam
,
PamFlag
,
PamError
,
PamLibExt
};
fn
pam_sm_prepare
(
argv
:
&
Vec
<
String
>
)
->
Config
{
fn
pam_sm_prepare
(
argv
:
&
Vec
<
String
>
)
->
(
Cache
,
Config
)
{
let
conf_args
=
argv_to_config
(
argv
);
let
conf
=
get_config
(
Some
(
conf_args
));
...
...
@@ -38,14 +38,16 @@ fn pam_sm_prepare(argv: &Vec<String>) -> Config {
}
setup_log
(
log_level
);
return
conf
;
let
cache
=
Cache
::
new
(
"pam"
);
return
(
cache
,
conf
);
}
struct
PamOidc
;
impl
PamServiceModule
for
PamOidc
{
fn
authenticate
(
pamh
:
Pam
,
_
:
PamFlag
,
argv
:
Vec
<
String
>
)
->
PamError
{
let
conf
=
pam_sm_prepare
(
&
argv
);
let
(
mut
cache
,
conf
)
=
pam_sm_prepare
(
&
argv
);
if
conf
.get_str
(
"pam.flow"
)
.unwrap
()
==
"password"
{
debug!
(
"Starting Resource Owner Password Credentials OAuth flow"
);
...
...
@@ -90,7 +92,7 @@ impl PamServiceModule for PamOidc {
match
get_access_token_password
(
&
conf
,
"pam"
,
username
.to_string
(),
password
.to_string
(),
PamError
::
SERVICE_ERR
,
PamError
::
AUTH_ERR
)
{
Ok
(
t
)
=>
{
info!
(
"Authenticated {} using Resource Owner Password Grant"
,
username
);
get_
cache
()
.save_user_token
(
&
username
.to_string
(),
t
.into
());
cache
.save_user_token
(
&
username
.to_string
(),
t
.into
());
return
PamError
::
SUCCESS
;
},
Err
(
e
)
=>
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment