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

fix:chown socket file to belong to nobody #54

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
24 changes: 24 additions & 0 deletions apisix/runner/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self):
init socket config handler
"""
self.file = "/tmp/runner.sock"
self.owner = "nobody"

@property
def file(self):
Expand All @@ -36,6 +37,14 @@ def file(self):
"""
return self._file

@property
def owner(self):
"""
get config owner for socket
:return:
"""
return self._owner

@file.setter
def file(self, file: str) -> None:
"""
Expand All @@ -44,6 +53,15 @@ def file(self, file: str) -> None:
:return:
"""
self._file = file.replace("unix:", "")

@owner.setter
def owner(self, owner: str) -> None:
"""
set config owner for socket
:param owner:
:return:
"""
self._owner = owner


class _ConfigLogging:
Expand Down Expand Up @@ -128,6 +146,12 @@ def _loading_config(self, config_path: str, config_name: str):
socket_file = self._get_env_config(socket.get("file"))
if socket_file:
self.socket.file = socket_file
# owner config
socket_owner = self._get_env_config(socket.get("owner"))
if socket_owner:
self.socket.owner = socket_owner
else:
self.socket.owner = "nobody"

# logging config
logger = configs.get("logging", {})
Expand Down
3 changes: 3 additions & 0 deletions apisix/runner/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
import socket

from pwd import getpwnam
from threading import Thread as NewThread
from apisix.runner.server.handle import Handle as NewServerHandle
from apisix.runner.server.protocol import Protocol as NewServerProtocol
Expand Down Expand Up @@ -82,6 +83,8 @@ def __init__(self, config: NewServerConfig):
os.remove(self.fd)
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.bind(self.fd)
user = getpwnam(config.socket.owner)
os.chown(self.fd, user.pw_uid, user.pw_gid)
self.sock.listen(1024)

self.logger = NewServerLogger(config.logging.level)
Expand Down
2 changes: 2 additions & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

socket:
file: $env.APISIX_LISTEN_ADDRESS # Environment variable or absolute path
# owner: somebody # optional Environment variable or user name


logging:
level: warn # error warn info debug
14 changes: 10 additions & 4 deletions docs/en/latest/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ title: Getting started
-->

## Overview

This document explains how to use Python Runner

## Prerequisites

* Python 3.7+
* APISIX 2.7.0+

- Python 3.7+
- APISIX 2.7.0+

## Installation

Expand All @@ -46,12 +46,14 @@ $ make install
> Development Mode

#### Run APISIX Python Runner

```bash
$ cd /path/to/apisix-python-plugin-runner
$ make dev
```

#### Modify APISIX configuration file

```bash
$ vim /path/to/apisix/conf/config.yaml
apisix:
Expand All @@ -66,6 +68,7 @@ ext-plugin:
> Production Mode

#### Modify APISIX configuration file

```bash
$ vim /path/to/apisix/conf/config.yaml
apisix:
Expand All @@ -83,19 +86,22 @@ ext-plugin:
$ vim /path/to/apisix-python-plugin-runner/conf/config.yaml
socket:
file: $env.APISIX_LISTEN_ADDRESS # Environment variable or absolute path
owner: $env.SOCKET_OWNER # optional Environment variable or user name. Default: 'nobody' (default user for Apisix runners)

logging:
level: debug # error warn info debug
```

### Start or Restart APISIX

```bash
$ cd /path/to/apisix
# Start or Restart
$ ./bin/apisix [ start | restart ]
```

### Configure APISIX Routing Rule

```bash
$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
Expand All @@ -116,8 +122,8 @@ $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f
}'
```


# Testing

```bash
$ curl http://127.0.0.1:9080/get -i
HTTP/1.1 200 OK
Expand Down
23 changes: 23 additions & 0 deletions tests/conf/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

socket:
file: $env.APISIX_LISTEN_ADDRESS # Environment variable or absolute path


logging:
level: warn # error warn info debug
24 changes: 24 additions & 0 deletions tests/conf/config_with_owner.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

socket:
file: $env.APISIX_LISTEN_ADDRESS # Environment variable or absolute path
owner: somebody # optional Environment variable or user name


logging:
level: warn # error warn info debug
6 changes: 5 additions & 1 deletion tests/runner/server/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ def test_config_default():
config.socket.file = "/test/runner.sock"
assert config.socket.file == "/test/runner.sock"

assert config.socket.owner == "nobody"


def test_config_custom():
config = NewServerConfig("%s" % os.path.abspath(os.path.join(os.getcwd())), "config.yaml")
config = NewServerConfig("%s" % os.path.abspath(os.path.join(os.getcwd(), "tests")), "config_with_owner.yaml")

config.logging.level = "NOTSET"
assert config.logging.level == logging.NOTSET
Expand All @@ -56,3 +58,5 @@ def test_config_custom():

config.socket.file = "/test/runner.sock"
assert config.socket.file == "/test/runner.sock"

assert config.socket.owner == "somebody"
11 changes: 8 additions & 3 deletions tests/runner/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@
# limitations under the License.
#

import os
from pwd import struct_passwd
import socket
import logging
from unittest.mock import patch
from apisix.runner.server.server import Server as RunnerServer
from apisix.runner.server.server import RPCRequest as RunnerRPCRequest
from apisix.runner.server.logger import Logger as RunnerServerLogger
from apisix.runner.server.config import Config as RunnerConfig


def test_server(capsys):
config = RunnerConfig()
@patch('pwd.getpwnam', return_value=struct_passwd({"pw_name":"nobody", "pw_passwd":"x", "pw_uid":65534, "pw_gid":65534, "pw_gecos":"nobody", "pw_dir":"/", "pw_shell":"/sbin/nologin"}))
@patch('os.chown')
def test_server(mock_chown,mock_getpwnam,capsys):
config = RunnerConfig("%s" % os.path.abspath(os.path.join(os.getcwd(),"tests")), "config.yaml")
server = RunnerServer(config)
mock_chown.assert_called_with("/tmp/runner.sock",65534,65534)
del server
captured = capsys.readouterr()
assert captured.out.find("listening on unix") != -1
Expand Down