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 an option to create a default project for a new user #16731

Open
zv0n opened this issue Apr 21, 2022 · 4 comments
Open

Add an option to create a default project for a new user #16731

zv0n opened this issue Apr 21, 2022 · 4 comments
Assignees
Labels
kind/requirement New feature or idea on top of harbor

Comments

@zv0n
Copy link

zv0n commented Apr 21, 2022

Is your feature request related to a problem? Please describe.
At our company we would like to provide users with a place where to store their docker images. We want each user to have their own project (mainly for storage quotas purposes), however it is not feasible to create a new project for every user that wants to use our harbor instance.

Describe the solution you'd like
We would like to have an option to create a new project when a new user is created. Our instance is connected to an OIDC, so we would like for the user to log in through OIDC and have their project ready as soon as possible after they log in for the first time.

If this behavior isn't simple to implement we would also be okay with addition of an option that causes a webhook call with user information in it. We would then use this call to create a new project through Harbor API.

I hope at least one of these approaches is feasible to implement into the project.

@Vad1mo Vad1mo added the kind/requirement New feature or idea on top of harbor label Apr 21, 2022
@Vad1mo
Copy link
Member

Vad1mo commented Apr 21, 2022

Harbor doesn't currently support that, but we created a stored procedure at Container-Registry for c8n.io. would love to see this working out of the box but in the meantime

--
-- Create for each new user a new project same as username with 1G of quota.
-- Customization for C8N, each new user should have his own project with a quota.

CREATE OR REPLACE FUNCTION create_project_for_new_user()
    RETURNS TRIGGER AS
$$

DECLARE
    new_project_id int;
BEGIN
    IF NEW.user_id IS NULL THEN
        RAISE EXCEPTION 'NEW.user_id has to be set';
    END IF;

    INSERT INTO public.project (owner_id, name)
    VALUES (1, lower(regexp_replace(NEW.username, '\W+', '', 'g')))
    RETURNING project.project_id INTO new_project_id;

    INSERT INTO public.project_member (project_id, entity_id, entity_type, role)
    VALUES (new_project_id, NEW.user_id, 'u', 4);

    INSERT INTO public.quota (reference, reference_id, hard)
    VALUES ('project', new_project_id, '{"storage": 1073741824}');

    INSERT INTO public.quota_usage (reference, reference_id, used)
    VALUES ('project', new_project_id, '{"storage": 0}' );

    RETURN NEW;
END;

$$ LANGUAGE plpgsql;

DO
$$
    BEGIN
        IF NOT EXISTS
            (SELECT *
             FROM information_schema.triggers
             WHERE event_object_table = 'harbor_user'
               AND trigger_name = 'new_harbor_user_create_project_trigger')
        THEN
            CREATE TRIGGER new_harbor_user_create_project_trigger
                AFTER INSERT
                ON harbor_user
                FOR EACH ROW
            EXECUTE PROCEDURE create_project_for_new_user();
        END IF;
    END;
$$

@zv0n
Copy link
Author

zv0n commented Apr 21, 2022

Thank you for the solution @Vad1mo !

I'm not familiar with PostgreSQL much, can I just paste this into the interactive console that's started with psql or does it need to be saved as an SQL script?

@Vad1mo
Copy link
Member

Vad1mo commented Apr 22, 2022

yes

@github-actions
Copy link

This issue is being marked stale due to a period of inactivity. If this issue is still relevant, please comment or remove the stale label. Otherwise, this issue will close in 30 days.

@github-actions github-actions bot added the Stale label Jun 24, 2022
@stonezdj stonezdj removed the Stale label Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/requirement New feature or idea on top of harbor
Projects
None yet
Development

No branches or pull requests

4 participants