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

Fixes #638. Auth logic CLI for oak use-cases. #730

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

ABSL_FLAG(std::string, policy_rules, "", "file containing policy rules");
ABSL_FLAG(std::string, datalog_file, "", "path to policy directory");
ABSL_FLAG(std::optional<bool>, skip_declarations, std::nullopt,
"true: Need to declare all the relations; false: No need to declare "
"pre defined relations");

constexpr char kUsageMessage[] =
"This tool takes a file containing policy, converts it to datalog and "
Expand Down Expand Up @@ -73,6 +76,10 @@ int main(int argc, char* argv[]) {
LOG(ERROR) << "Error reading policy rules file: " << policy_rules.status();
return 2;
}
const std::optional<bool> skip_declarations_flag =
absl::GetFlag(FLAGS_skip_declarations);
const bool skip_declarations =
skip_declarations_flag.has_value() && skip_declarations_flag.value();

std::filesystem::path datalog_file_path(absl::GetFlag(FLAGS_datalog_file));

Expand All @@ -82,7 +89,8 @@ int main(int argc, char* argv[]) {
DatalogProgram datalog_program =
raksha::ir::auth_logic::LoweringToDatalogPass::Lower(program);
std::string datalog_policy =
raksha::ir::auth_logic::SouffleEmitter::EmitProgram(datalog_program);
raksha::ir::auth_logic::SouffleEmitter::EmitProgram(datalog_program,
skip_declarations);

std::ofstream datalog_file(
datalog_file_path, std::ios::out | std::ios::trunc | std::ios::binary);
Expand Down
20 changes: 20 additions & 0 deletions src/ir/auth_logic/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,23 @@ cc_test(
"@absl//absl/strings",
],
)

sh_test(
name = "authorization_logic_prototype",
srcs = [
"auth_logic_prototype.sh",
],
args = [
"$(location //src/backends/policy_engine/souffle:raksha_policy_datalog_emitter)",
"$(location //src/ir/auth_logic/tests:simple_query.auth)",
],
data = [
"//src/backends/policy_engine/souffle:raksha_policy_datalog_emitter",
"//src/ir/auth_logic/tests:simple_query.auth",
"//third_party/mcpp",
"//third_party/souffle:main",
],
env = {
"SOUFFLE_BIN": "$(rootpath //third_party/souffle:main)",
},
)
51 changes: 51 additions & 0 deletions src/ir/auth_logic/auth_logic_prototype.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash
#
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-------------------------------------------------------------------------------

function printUsageAndExit() {
echo "Usage: "
echo " auth_logic_prototype.sh \ "
echo " <auth_logic_file> <output_souffle_file> <output_query_directory>"
exit 1
}

CMD_ARG=$1
AUTH_LOGIC_FILE_ARG=$2
#Following args are optional
OUTPUT_SOUFFLE_FILE_ARG=${3:-$(mktemp)}
OUTPUT_QUERY_DIR_ARG=${4:-$(mktemp -d)}
#To-do make below args optional
#TEST_ONLY=${5:-0}

$CMD_ARG --policy_rules=$AUTH_LOGIC_FILE_ARG --datalog_file=$OUTPUT_SOUFFLE_FILE_ARG --skip_declarations=true

if [ -z "$SOUFFLE_BIN" ]
then
echo "Set SOUFFLE_BIN"
exit 1
else
echo $SOUFFLE_BIN
fi

PATH=$PATH:third_party/mcpp
$SOUFFLE_BIN $OUTPUT_SOUFFLE_FILE_ARG -D $OUTPUT_QUERY_DIR_ARG/

echo $(cat $OUTPUT_SOUFFLE_FILE_ARG)
echo $(ls $OUTPUT_QUERY_DIR_ARG)

#Compare diff for test TEST_ONLY

30 changes: 18 additions & 12 deletions src/ir/auth_logic/souffle_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ namespace raksha::ir::auth_logic {

class SouffleEmitter {
public:
static std::string EmitProgram(const datalog::Program& program) {
static std::string EmitProgram(const datalog::Program& program,
bool skip_declarations = false) {
SouffleEmitter emitter;
std::string body = emitter.EmitProgramBody(program);
std::string outputs = emitter.EmitOutputs(program);

std::string declarations = emitter.EmitRelationDeclarations(program);
std::string type_declarations = emitter.EmitTypeDeclarations(program);
std::string declarations =
emitter.EmitRelationDeclarations(program, skip_declarations);
std::string type_declarations =
emitter.EmitTypeDeclarations(program, skip_declarations);
return absl::StrCat(std::move(type_declarations), "\n",
std::move(declarations), "\n", std::move(body), "\n",
std::move(outputs));
Expand Down Expand Up @@ -141,12 +143,14 @@ class SouffleEmitter {
});
}

std::string EmitRelationDeclarations(const datalog::Program& program) {
std::string EmitRelationDeclarations(const datalog::Program& program,
const bool skip_declarations) {
std::vector<std::string> declaration_strings;
for (const auto& declaration : program.relation_declarations()) {
CHECK(!GetRelationsToNotDeclare().empty());
if (GetRelationsToNotDeclare().find(declaration.relation_name()) !=
GetRelationsToNotDeclare().end())
if (!skip_declarations &&
GetRelationsToNotDeclare().find(declaration.relation_name()) !=
GetRelationsToNotDeclare().end())
continue;
declaration_strings.push_back(
absl::StrCat(".decl ", declaration.relation_name(), "(",
Expand All @@ -156,15 +160,17 @@ class SouffleEmitter {
return absl::StrJoin(declaration_strings, "\n");
}

std::string EmitTypeDeclarations(const datalog::Program& program) {
std::string EmitTypeDeclarations(const datalog::Program& program,
const bool skip_declarations) {
absl::flat_hash_set<std::string> type_names;
for (const auto& declaration : program.relation_declarations()) {
for (const auto& argument : declaration.arguments()) {
if (argument.argument_type().kind() !=
datalog::ArgumentType::Kind::kCustom)
if (!skip_declarations && argument.argument_type().kind() !=
datalog::ArgumentType::Kind::kCustom)
continue;
if (GetRelationsToNotDeclare().find(argument.argument_type().name()) !=
GetRelationsToNotDeclare().end())
if (!skip_declarations &&
GetRelationsToNotDeclare().find(argument.argument_type().name()) !=
GetRelationsToNotDeclare().end())
continue;
type_names.insert(absl::StrCat(
".type ", argument.argument_type().name(), " <: symbol"));
Expand Down
25 changes: 25 additions & 0 deletions src/ir/auth_logic/tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#-------------------------------------------------------------------------------
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-------------------------------------------------------------------------------
package(
default_visibility = ["//src:__subpackages__"],
features = ["layering_check"],
licenses = ["notice"],
)

exports_files([
"simple_query.auth",
])
3 changes: 3 additions & 0 deletions src/ir/auth_logic/tests/simple_query.auth
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.decl mayA(prin : Principal)
"UserA" says mayA("NotifierA").
may_notifierA = query "UserA" says mayA(NotifierA)?