Skip to content

Instantly share code, notes, and snippets.

@EmperorYP7
Last active September 12, 2023 07:08
Show Gist options
  • Save EmperorYP7/d48ef6840bbb0ccc63ea4a5a2d19f67d to your computer and use it in GitHub Desktop.
Save EmperorYP7/d48ef6840bbb0ccc63ea4a5a2d19f67d to your computer and use it in GitHub Desktop.
Google Summer of Code 2021 Project Report

Google Summer of Code 2021 Final Work Product


Group 15


Introduction

  • Name: Yash Pandey (@EmperorYP7)
  • Organisation: Casbin
  • Project link: https://summerofcode.withgoogle.com/projects/#6057909131149312
  • Repository link: https://github.com/casbin/casbin-cpp
  • Project Name: Benchmarking, Python bindings and Migrating to CTest for Casbin-CPP
  • Project Summary: Casbin is an authorization library that extends its features to implement Access Control Lists, Role-Based Access Control, and Attribute-Based Access Control models in various programming languages to its clients. Casbin's Core Engine is written in GoLang. Casbin-CPP has obvious benefits of speed and efficiency compared to its implementation in other languages and thus, benchmarking is vital for the project to stand out from the rest. Python is the most versatile as well as the most used programming language and has huge community support. Casbin-CPP has the potential to support new PyCasbin to compound the benefits of both languages through language bindings and extension libraries. Currently, the project uses Microsoft Unit Testing Framework for C++ for testing and Microsoft’s Azure DevOps pipelines for CI. CTest is truly cross-platform and can be configured using GitHub Actions for consistent and better CI. Project Ideas are as follows:
    • Modernizing the project with C++17 standard and Google’s benchmarking tool
    • Implement Python bindings for Casbin-CPP using pybind11 library
    • Implement testing based on CTest and set up workflows for Continuous Integration through GitHub Actions


About the Project

Casbin is an authorization libary which provides various authorization models upon which the client can build auth interfaces for their apps.

The project was using the Visual Studio workflow to build and test the project. However, the demographics of the potential clients include people who use UNIX based operating systems for development. Thus, we needed a new build system to handle the generation, testing and packaging of the library for all platforms to make it better. This can be done using CMake build system along with CTest.

Here is the current build tree of the project:

The project also involved setting up all the tests that were defined in the MS Unit Testing framework to Googletest and setting up CI workflow through GitHub Actions. A typical test may look like this:

#include <gtest/gtest.h>
#include <casbin/casbin.h>
#include "config_path.h"

TEST(TestEnforcer, TestVectorParams) {
    casbin::Enforcer e(basic_model_path, basic_policy_path);

    ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
    ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
    // ... and so on
}

The project also needed some performance metrics in the form of benchmarks. I developed the benchmarking workflow for the project with Google's benchmarking tool along with continuous testing with GitHub Actions. Here's a simple benchmark for casbin::Enforcer::Enforce().

#include <benchmark/benchmark.h>
#include <casbin/casbin.h>
#include "config_path.h"

static void BM_Enforcer(benchmark::State& state) {
    casbin::CachedEnforcer e(basic_model_path, basic_policy_path);

    for(auto _ : state)
        e.Enforce({ "alice", "data1", "read" });
}

The project needs powerful python bindings for supporting PyCasbin-on-CPP which combines the advantages of two different programming languages. This can be made with pybind11 library. A sample binding may look like this:

#include <pybind11/pybind11.h>
#include <casbin/casbin.h>

namespace py = pybind11;

void bindPyEnforcer(py::module& m) {
    py::class_<casbin::Enforcer>(m, "Enforcer")
        .def(py::init<const std::string &, const std::string &>(), "")
        .def("Enforce", &casbin::Enforcer::Enforce);
}

Contributions

Prior to GSoC

I've been contributing to Casbin-CPP prior to GSoC coding period. This is when I got introduced to the project. I tackled some good first issues and learnt about the codebase. Major contributions include the implementation of SyncedEnforcer along with the unit tests. Here are my contributions:

# Title Status
casbin-cpp#82 feat: Replaced pragma with include guards badge
casbin-cpp#83 feat: Implemented synced enforcer badge
casbin-cpp#84 Python bindings to facilitate PyCasbin-on-CPP badge
casbin-cpp#90 feat: Added async ticker badge
casbin-cpp#92 feat: Replaced travis with GitHub Actions badge
casbin-cpp#93 feat: Added translation unit for synced enforcer badge
casbin-cpp#94 feat: Added Tests for SyncedEnforcer badge
casbin-cpp#96 Upgrade to C++17 badge
casbin-cpp#98 fix: Build on MS Visual Studio badge

Contributions during GSoC

Here is the list of contributions I made during the coding period of GSoC i.e. 8th June 2021 to 18th August 2021.

# Title Status
casbin-cpp#99 Configure CMake build system 🏗️ badge
casbin-cpp#101 feat: Initiate CMake configuration badge
casbin-cpp#103 feat: Completion of Enforcer API badge
casbin-cpp#104 fix: Taking in parameters by const reference badge
casbin-cpp#105 feat: Added ABAC entity wrapper badge
casbin-cpp#106 feat: Added implementation and tests for UpdatePolicy API badge
casbin-cpp#107 feat: Initiated CTest setup badge
casbin-cpp#108 test: Management API, Utility methods and more badge
casbin-cpp#109 test: Added RBAC, Role Manager and removed MS Unit Tests badge
casbin-cpp#110 feat: Implemented ABAC in Internal API badge
casbin-cpp#112 feat: Inherit std::exception badge
casbin-cpp#113 chore: Updated docs for build instructions badge
casbin-cpp#114 feat: Initiated benchmark workflow badge
casbin-cpp#116 test: Benchmarks for CachedEnforcer badge
casbin-cpp#117 Error while using casbin::Enforcer::AddPolicies() badge
casbin-cpp#118 test: Benchmarks for ManagementAPI badge
casbin-cpp#119 Memory leakage while using Enforcer::AddPolicy and RBAC workflows badge
casbin-cpp#120 test: Benchmarks for Model and RoleManager badge
casbin-cpp#121 chore: Cleanup badge
casbin-cpp#122 feat: Streamlined types badge
casbin-website#256 feat: Added benchmarks for Casbin-CPP badge
casbin-cpp#123 feat: Initiated pybind11 Configuration badge
casbin-cpp#124 chore: Util cleanup badge
casbin-cpp#125 chore: Model Cleanup badge
casbin-cpp#127 chore: Config and enforcer cleanup badge
casbin-cpp#128 feat: Added python bindings for casbin::Enforcer badge
casbin-cpp#129 feat: Added bindings for CachedEnforcer badge
casbin-cpp#130 feat: Added bindings for casbin::ABACData badge
casbin-cpp#133 feat: Added tests for Python Bindings badge
casbin-cpp#134 Add tests for python bindings badge
casbin-cpp#135 feat: Added bindings for casbin::Model badge
casbin-cpp#136 [Test] Error in locating Test Entities in Xcode badge
casbin-cpp#137 feat: Added config_path for test entities badge
casbin-cpp#138 refactor: Exported targets for easy access badge
casbin-cpp#139 feat: Added python bindings for casbin::Config badge
casbin-cpp#141 chore: Updated project config badge
casbin-cpp#142 feat: Added bindings for casbin::SyncedEnforcer badge
casbin-cpp#145 docs: Updated documentation and version badge
casbin-cpp#146 docs: Added documentation for python bindings badge

Weekly blogs/reports can be found at https://gsoc.casbin.org.

I also documented my progress in a separate gist.

The entirety of my contributions during the coding period includes 31 Pull Requests (30 Merged, 1 Closed) and 7 issues which accounts for 18,867 lines of code added and 6,730 lines of code deleted in casbin-cpp. The entire list of my contributions in casbin-cpp can be accessed here.

Screenshot


Outcome

The project's directory structure is set and CMake build system has been configured successfully and is ready to be used across all platforms.

Testing for casbin-cpp has been completed successfully. There are 57 individual and disjoint tests executed through CTest. The CI now tests the library for every platform on PR and push.

The benchmarks are functional and the corresponding CI is up and running. Here is the output of casbin_benchmark on my system:

Screenshot 2021-08-20 at 2 20 10 AM


Python bindings for casbin-cpp are now ready to be used as pycasbin module. Documentation for installation and usage are up as well. Here is a summary on how to use the pycasbin module within a python file:

pycasbin

The CI test for PyCasbin-on-CPP are up and running through GitHub Actions. Here is the output of the tests:

Screenshot 2021-08-20 at 8 10 10 PM


I also made a repository to demonstrate how to integrate casbin into a CMake project for the clients. Here is the link to the same: https://github.com/EmperorYP7/casbin-CMake-setup


What's next?

All the primary goals of my GSoC proposal are completed. However, there is a huge scope for improving the product.

Here are a few things which can be done in the project for any future contributor:

  • Functional tests for casbin::SyncedEnforcer. The problem arised due to ambiguity in handling multi-threaded tests in GoogleTest.

  • Benchmarking for casbin::SyncedEnforcer. This one is related to the problem described above.

  • Packaging of pycasbin as discussed in casbin-cpp#146. It is still difficult to install python bindings and we need some workflow to upload and update the package in pypi.org.

  • Completion of the implementation of ABAC model within the internal API of casbin-cpp.

  • Inspecting and improving the metrics shown by the benchmarks for regular casbin::Enforcer as discussed in casbin-website#255.

I plan to work on these issues after the final evaluations are completed successfully and keep contributing to the project and help the organisation in their growth.

Ackowledgement

I'd like to thank Google for organizing such an amazing program for us and that it did create an impact on my immediate surroundings and peers to participate and contribute to opensource.

Apart from that, I'd like to thank my mentor Yang Luo (@hsluoyz) for their constant support and guidance and Joey (@xcaptain) for their suggestions during the program.

This was a great experience for me! I learned a lot about software engineering and sharpen my researching skills through this opportunity.


📫 Connect with me

@Young-Flash
Copy link

excellent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment