Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sudo and mu auth scope #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ The template supports the following environment variables:

- `MU_SPARQL_ENDPOINT`: SPARQL endpoint URL. Default: `http://database:8890/sparql`
- `MU_SPARQL_TIMEOUT`: timeout (in seconds) for SPARQL queries. Default: 60 seconds.
- `ALLOW_MU_AUTH_SUDO`: Allow sudo queries when the service requests it.
- `DEFAULT_MU_AUTH_SCOPE`: Default mu-auth-scope to use for calls.
- `LOG_LEVEL`: the level of logging (default: `info`, values: `debug`, `info`, `warn`, `error`, `fatal`).
- `USE_LEGACY_UTILS`: when enabled (using `"true"` or `"yes"`) legacy utils from v2 will be included in the root file so they can be used as before (e.g. `query` instead of `Mu::query`). Default: `"true"`
- `PRINT_DEPRECATION_WARNINGS`: Deprecation warnings will be printed for each usage of a legacy util. Default: `"true"`.
Expand Down
25 changes: 19 additions & 6 deletions mu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@ def log
@log
end

def query(query)
def query(query, **options)
log.info "Executing query: #{query}"
sparql_client.query query
@sparql_client = sparql_client(**options)
erikap marked this conversation as resolved.
Show resolved Hide resolved
@sparql_client.query query
end

def sparql_client
options = {}
def sparql_client(**options)
if Mu::truthy? options[:sudo]
if Mu::truthy? ENV['ALLOW_MU_AUTH_SUDO']
options[:headers] = { 'mu-auth-sudo': 'true' }
else
log.error "Error, sudo request but service lacks ALLOW_MU_AUTH_SUDO header"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also raise an error here instead of only logging one? Query will now still be executed with a non-sudo SPARQL client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I go with raise?

raise "Error, sudo request but service lacks ALLOW_MU_AUTH_SUDO header"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems more in line with the JS template to me 👍

end
end
if options[:scope]
options[:headers] = { 'mu-auth-scope': options[:scope] }
elsif ENV['DEFAULT_MU_AUTH_SCOPE']
options[:headers] = { 'mu-auth-scope': ENV['DEFAULT_MU_AUTH_SCOPE'] }
end
if ENV['MU_SPARQL_TIMEOUT']
options[:read_timeout] = ENV['MU_SPARQL_TIMEOUT'].to_i
end
Expand Down Expand Up @@ -71,9 +83,10 @@ def truthy? value
["true", "yes", "1"].include?(value && value.to_s.downcase)
end

def update(query)
def update(query, **options)
log.info "Executing query: #{query}"
sparql_client.update query
@sparql_client = sparql_client(**options)
erikap marked this conversation as resolved.
Show resolved Hide resolved
@sparql_client.update query
end

def update_modified(subject, modified = DateTime.now)
Expand Down