How to add swagger security defintion to all of my rpc methods in grpc-gateway

service ActorDefinitionServices {
    rpc GetActorDefinition(GetActorDefinitionRequest) returns (GetActorDefinitionResponse) {
        option (google.api.http) ={
            get: "/v1/reverseetl/actor-definition/{id}"
        };
        option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
            security: {
                security_requirement: {
                    key: "ApiKeyAuth",
                    value: {
                    },
                }
                security_requirement: {
                    key:"WorkspaceIDHeader",
                    value:{
                    },
                }
            }
        };

    }
    rpc CreateActor(CreateActorRequest) returns (ActorResponse) {
        option (google.api.http) ={
            post: "/v1/reverseetl/actor"
            body: '*'
        };
        option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
            security: {
                security_requirement: {
                    key: "ApiKeyAuth",
                    value: {
                    },
                }
                security_requirement: {
                    key:"WorkspaceIDHeader",
                    value:{
                    },
                }
            }
        };
    }
    rpc UpdateActor(UpdateActorRequest) returns (ActorResponse) {
        option (google.api.http) ={
            patch: "/v1/reverseetl/actor/{id}"
            body: 'body'
        };
        option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
            security: {
                security_requirement: {
                    key: "ApiKeyAuth",
                    value: {
                    },
                }
                security_requirement: {
                    key:"WorkspaceIDHeader",
                    value:{
                    },
                }
            }
        };
    }
    
}

these are my grpc services and i am using grpc-gateway to create http endpoints as well. I am also using swagger docs. So I want to apply security to all my rpc methods but in order to do that I need to define it in every rpc method. I dont want to do that . Is there any possible way to write it once and it will apply it on all of my services.

i have try this

option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
    info: {
      title: "Actor Definition";
      version: "1.0";
      contact: {
        name: "Actor definiton POC project";
        url: "https://github.com/Razihmad/actors";
        email: "[email protected]";
      };
    };
    security_definitions:{
        security:{
            key: "ApiKeyAuth",
            value: {
                description: "JWT token for authorization ( Bearer token )"
                type: TYPE_API_KEY,
                name: "Authorization",
                in: IN_HEADER
            },
        }
        security: {
            key:"WorkspaceIDHeader",
            value:{
                description: "Workspace ID is required for most of the endpoints",
                type: TYPE_API_KEY,
                name: "X-GC-Current-Workspace-ID",
                in: IN_HEADER
            },
        }
    }
};
but it is applying on upper level but not on individual route.

Leave a Comment