如何解决python-win32security通过跨域访问为NAS中的文件夹添加ACL
我正在研究一个用例,以创建一个文件夹并添加安全组。我正在使用下面的代码。当我手动执行此操作以访问共享路径时,我们输入凭据并创建一个文件夹Post,一旦我单击“安全性”选项卡,它会再次提示输入凭据,并填充相同的凭据和安全组。这是因为期望从其他域访问共享位置。现在,当我尝试使用以下代码通过python执行此操作时,我能够创建文件夹,但由于脚本是从其他域中的服务器运行的,因此无法添加安全组。
错误(1332,LookupAccountName'没有完成帐户名和安全ID之间的映射。)
因此,基本上,在访问权限设置相同的安全选项卡时,我们如何设置权限。
请帮助。
class Create(Resource):
def post(self):
# Get JSON arguments from Payload shared NAS path,directorname groupname with read access and right access
parentdir = request.json.get("path")
dirname = request.json.get("name")
readGroup = request.json.get("readGroup")
# Access the NAS path through NAS credentails
class Impersonate:
def __init__(self,user,password):
#Update domain to access the shared NAS
self.domain_name = "domain"
self.user = user
self.password = password
logging.debug("Credentials Received: {} ".format(self.user))
def logon(self):
self.handle=win32security.LogonUser(self.user,self.domain_name,self.password,win32con.LOGON32_LOGON_INTERACTIVE,win32con.LOGON32_PROVIDER_DEFAULT)
win32security.ImpersonateLoggedOnUser(self.handle)
def logoff(self):
win32security.RevertToSelf() #terminates impersonation
self.handle.Close() #guarantees cleanup
if __name__ == "__main__":
#update username and password of the NAS path below within quotes
a=Impersonate('user','Password')
try:
a.logon() #Logon to NAS path with supplied credentails.
try:
logging.debug("Sucessfully connectd to NAS path {} ".format(parentdir))
# makedirs create directory recursively
os.makedirs(path)
try:
groupr,domain,type = win32security.LookupAccountName ("",readGroup)
sd = win32security.GetFileSecurity(path,win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()
dacl.AddAccessAllowedAce(win32security.ACL_REVISION,win32con.GENERIC_READ,groupr)
#os.makedirs(path)
except OSError as e:
if e.errno == errno.EEXIST:
print(e)
resp = Response('{} fileshare creation created,adding security group {} with read permessions failed. Error:{}'.format(dirname,groupr,e))
print (resp)
resp.status_code = 201
return resp
except OSError as error:
print(error)
resp = Response('{} fileshare creation failed. Error is {} '.format(dirname,error))
print (resp)
resp.status_code = 300
return resp
#return ("Fileshare creation failed: {} ".format(dirname))
except Exception as error1:
print(error1)
logging.error("Failed to connect to NAS path{},Error: {} ".format(parentdir,error1))
resp = Response('Could not connect to UNC Shared path. Error{}'.format(error1))
print (resp)
resp.status_code = 201
return resp
a.logoff()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。