简体   繁体   English

允许python应用程序为任何用户创建和访问文件,但不允许用户直接访问文件

[英]Allow python application to create and access files for any user, but not the users to acces the files directly

I am developing an application in python, and I'm struggling with file permissions. 我正在用python开发应用程序,而我在文件权限方面苦苦挣扎。

My application is creating, accessing and modifying several files using the shelve and logging modules. 我的应用程序正在使用货架日志记录模块创建,访问和修改多个文件。

This application will be on a server, and will be used by several users belonging to different groups. 该应用程序将位于服务器上,并将被属于不同组的多个用户使用。

My problem is that the files are marked as owned by the user who launched the application for the first time, thus creating the files, and after that, when another user launches the application he does not have the needed rights to access the files, and the application crashes. 我的问题是,文件被标记为首次启动该应用程序的用户所拥有,从而创建了文件,此后,当另一个用户启动该应用程序时,他没有访问文件所需的权限,并且应用程序崩溃。

I could modify the permissions to allow all users to access and modify the files, but this wouldn't be really satisfying. 我可以修改权限,以允许所有用户访问和修改文件,但这并不能令人满意。

I found that using setuid I maybe could allow the application to access the files when launched by any user, but not allow the users to directly modify the files. 我发现使用setuid可能允许应用程序在任何用户启动时访问文件,但不允许用户直接修改文件。 This would be exactly what I need. 这正是我所需要的。

However, I couldn't find a way to modify the umask or the rights of the files created by shelve and logging modules. 但是,我找不到修改货架日志记录模块创建的文件的umask或权限的方法。

I think that maybe for the files accessed by shelve I can use os.umask to create the files before I access them, but it seems that wouldn't work for the files created by logging because I'm using a rotating file handler that might create files whenever the log files are full. 我认为也许对于搁置访问的文件,我可以在访问它们之前使用os.umask创建文件,但是似乎不适用于通过日志记录创建的文件,因为我使用的是旋转文件处理程序只要日志文件已满,就创建文件。

What would be the more pythonic way to handle that ? 有什么更Python的方式来处理呢?

Edit : 编辑:

As asked in the comments, here is a simple snippet of code that replicates my problem. 正如评论中所问的那样,这是复制我的问题的简单代码段。

#!/usr/bin/env python2.7
import logging

logger = logging.getLogger('test_logger')
logger.setLevel(logging.DEBUG)

fh = logging.FileHandler('logfile.log')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)

logger.info('Test')

Now if user A launches the application, a log file is created. 现在,如果用户A启动该应用程序,则会创建一个日志文件。 If user B then launches the application, i have an error because he hasn't the rights to access the log file. 如果用户B随后启动了该应用程序,则出现错误,因为他没有访问日志文件的权限。

The error is IOError : Permission denied [...]/logfile.log 错误是IOError:权限被拒绝[...] / logfile.log

EDIT 2 编辑2

Some additional information : 一些其他信息:

I do not have root access on the system where my application is, so setting a user specifically for my creating a user for the sole use of my application seems not possible. 我在我的应用程序所在的系统上没有root用户访问权限,因此专门为我创建一个仅可用于我的应用程序的用户设置用户似乎是不可能的。

The application will be rarely used, and by a small number of persons, thus I am for now assuming the won't access it at the same time. 该应用程序很少使用,只有少数人使用,因此,我现在假设不会同时访问它。

I don't need authentication and authorizations in the application. 我在应用程序中不需要身份验证和授权。 It should work exactly the same way for any user. 对于任何用户,它应该以完全相同的方式工作。

I think that, as suggested by goncalopp, creating a user and running the application as that user would be the best solution, but I am afraid that won't be possible. 我认为,按照goncalopp的建议,创建用户并以该用户身份运行应用程序将是最好的解决方案,但恐怕不可能。 I'm going to have to ask the sysadmin. 我将不得不问系统管理员。

It's not clear in your question what should happen if several users run the application at the same time. 您的问题尚不清楚,如果多个用户同时运行该应用程序会发生什么情况。 You'll have several processes writing to the same files simultaneously, unless you have a explicit lockfile, or some other synchronization mechanism. 除非您有显式的锁定文件或其他某种同步机制,否则您将有多个进程同时写入相同的文件。

Assuming you have sychronization solved, setuid may not be the best solution here. 假设您已解决了同步问题,那么setuid可能不是此处的最佳解决方案。 As python is a interpreted language, it's particularly difficult to setup, and the usual security considerations apply 由于python是一种解释型语言, 因此设置起来特别困难,并且通常需要考虑安全性

The usual unix solution to this kind of problem is to have your program run as a particular system user, which is created for that sole purpose . 解决此类问题的通常的unix解决方案是让您的程序以特定的系统用户身份运行,该用户是出于该特定目的而创建的 The program could either run as a daemon or in a cronjob. 该程序可以作为守护程序运行,也可以在cronjob中运行。 Using this method, if you need user interaction, you'll then have to explicitly expose operations to the users in your program, and have authentication and authorizarion mechanisms . 使用这种方法,如果需要用户交互,则必须向程序中的用户显式公开操作 ,并具有身份验证和授权机制

For interaction, you could, for example, use (unix or plain) sockets or watched/pool directories, depending on the nature of your program. 对于交互,您可以使用(unix或普通)套接字或受监视/池目录,具体取决于程序的性质。 For authentication, 对于身份验证,

Of course, all this means extra development work, which you may not be able to do at this point. 当然,所有这些都意味着额外的开发工作,您此时可能无法执行。 A workaround could be allowing passwordless sudo to a specific user and command 一种解决方法是允许特定用户和命令使用无密码sudo

You could try using the webbrowser api to open a file, 您可以尝试使用webbrowser api打开文件,

It dosen't need permissions 不需要权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM